How to Manually Enter Raw Data in R

Manually entering raw data into R is a relatively straightforward process. It involves creating a new data frame, assigning column names, and then entering the data values into the data frame. This is done by creating a vector containing the data values, and then assigning it to the appropriate column of the data frame. Once all of the data has been entered, the data frame can be used to analyze the data or produce graphs.


R is one of the most popular programming languages for working with data. But before we can work with data, we have to actually get data into R!

If you already have your data located in a CSV file or Excel file, you can follow the steps in these tutorials to import it into R:

However, sometimes you may want to manually enter into R. This tutorial explains how to do so.

Enter a Vector

We can use the following syntax to enter a single vector of numeric values into R:

#create vector of numeric values
numeric_values <- c(1, 3, 5, 8, 9)

#display class of vector
class(numeric_values)

[1] "numeric"
#display vector of numeric values
numeric_values

[1] 1 3 5 8 9

#return second element in vector
numeric_values[4]

[1] 8

We can use the same syntax to enter a vector of character values:

#create vector of character values
char_values <- c("Bob", "Mike", "Tony", "Andy")

#display class of vector
class(char_values)

[1] "character"

Enter a Data Frame 

We can use the following syntax to enter a data frame of a values in R:

#create data frame
df <- data.frame(team=c("A", "A", "B", "B", "C"),
                 points=c(12, 15, 17, 24, 27),
                 assists=c(4, 7, 7, 8, 12))

#display data frame
df

  team points assists
1    A     12       4
2    A     15       7
3    B     17       7
4    B     24       8
5    C     27      12

#display class of df
class(df)

[1] "data.frame"

#return value in fourth row and third column
df[4, 3]

[1] 8

Enter a Matrix

We can use the following syntax to enter a matrix of values in R:

#create matrix with two columns and five rows
points=c(12, 15, 17, 24, 27)
assists=c(4, 7, 7, 8, 12)

#column bind the two vectors together to create a matrix
mat <- cbind(points, assists)

#display matrix
mat

     points assists
[1,]     12       4
[2,]     15       7
[3,]     17       7
[4,]     24       8
[5,]     27      12

#display class of mat
class(mat)

[1] "matrix"

#return value in fourth row and second column
mat[4, 2]

assists 
      8

Note: A matrix requires each column to be the same type, unlike data frames.

You can find more R tutorials here.

x