Subsetting vector in R

In R you can subset various objects such as Vector, Matrix and List.

There are three operators that can be used to extract subsets of R objects.
• The[ operator always returns an object of the same class as the original. It can be used to select multiple elements of an object
• The [[ operator is used to extract elements of a list or a data frame. It can only be used to extract a single element and the class of the returned object will not necessarily be a list or data frame.
• The $ operator is used to extract elements of a list or data frame by literal name. Its semantics are similar to that of [[.

Subsetting a Vector

Vectors are basic objects in R and they can be subsetted using the [ operator

Extracting single element

> vowels<-c("a","e","i","o","u")
> vowels[1]            ## Extract the first element
[1] "a"
> x<-vowels[1]        ## Extract the first element as new variable x
> vowels[2]           ## Extract the second element
[1] "e"

Extracting multiple-element

The [ operator can be used to extract multiple elements of a vector bypassing the operator an integer
sequence. Here we extract the first four elements of the vector.

> vowels[1:4]
[1] "a" "e" "i" "o"
> x<-vowels[1:4]

The sequence does not have to be in order; you can specify any arbitrary integer vector.

vowels[c(1, 3, 4)]
[1] "a" "i" "o"
> x<-vowels[c(1, 3, 4)]

We can also pass a logical sequence to the [ operator to extract elements of a vector that satisfy a given condition. For example, here we want the elements of vowels that come lexicographically after the letter “a”.

> l<-vowels > "a"
> l

Another, more compact, way to do this would be to skip the creation of a logical vector and just subset the vector directly with the logical expression.

vowels1<-vowels[vowels > "a"]
> vowels1
[1] "e" "i" "o" "u"

In the next post let us see how to subset a matrix and list.

How to Manually Enter 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 Rm, 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

[1] "integer"


[1] "numeric"

#display vector of numeric values

[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


[1] 2 4 6 8 10

#return fifth element in vector

[1] 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

[1] "character"

#display vector of character values

[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

  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

[1] "data.frame"

#return value in third row and second column

[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

     goals assists
[1,] 15    4
[2,] 10    7
[3,] 8     10
[4,] 10    4
[5,] 16    5

#display class of mat

[1] "matrix" "array" 

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


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

You learn more about Data analysis using R here.