Manually Entering Data in R

R is one of the most popular and powerful programming languages used in data analytics.

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:

  • How to Import CSV Files into R
  • How to Import Excel Files into R

However, sometimes you may want to manually enter raw data into R. Let see how to do it

Entering a Vector in R

If you want to enter a single vector of numeric values into R, use the following syntax:

#create vector of numbers

#Method 1
>numbers <- (1:25)

#Method 2
>even <- c(2,4,6,8,10)

#display class of vector
>class(numbers)
[1] "integer"

>class(even)
[1] "numeric"

#display vector of numeric values
>numbers
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

>even
[1] 2 4 6 8 10

#return fifth element in vector
numbers[5]
[1] 5

>even[5]
[1] 10

Use the same syntax to enter a vector of character values as well

#create vector of character values
metal <- c("iron", "silver", "gold", "steel")

#display class of vector
>class(metal)
[1] "character"

#display vector of character values
>metal
[1] "iron" "silver" "gold" "steel"

Entering a DATAFRAME in R

If you want to create a data frame use the following syntax

#create data frame
>football_players <- data.frame(player=c("Salah", "Jesus", "Ozil", "Pepe", "Kane"),
 goals=c(15, 10, 8, 10, 16),
 assists=c(4, 7, 10, 4, 5))

#display data frame
>football_players
  player goals assists
1 Salah    15    4
2 Jesus    10    7
3 Ozil      8   10
4 Pepe     10    4
5 Kane     16    5

#display class of df
>class(football_players)
[1] "data.frame"

#return value in third row and second column
>football_players[3,2]
[1] 8

Entering a Matrix in R

If you want to create a matrix use the following syntax

#create matrix with two columns and five rows
>goals=c(15, 10, 8, 10, 16)
>assists=c(4, 7, 10, 4, 5)
#column bind the two vectors together to create a matrix
>mat <- cbind(goals, assists)
#display matrix
>mat
     goals assists
[1,] 15    4
[2,] 10    7
[3,] 8     10
[4,] 10    4
[5,] 16    5

#display class of mat
>class(mat)
[1] "matrix" "array" 

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

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

You learn more about Data analysis using R here.

Leave a Reply

Your email address will not be published.