3 Easy ways to Subset lists and vectors in R

Filed Under: R Programming
Subset Lists & Vectors In R

Hello readers! Today we will be focusing on 3 Easy ways to Subset lists and Vectors in R, in detail.

We often come across situations wherein we feel the need to segregate the data values into portions based on certain conditions. At this moment, we need a concrete function that can enable us to split the data values of a data structure without affecting the structure of the data.

So, we would be covering 3 easy techniques in this course of article to subset the data values of various R objects such as list, vector, etc.

Let us begin!


1. Subset of an R vector

In this example, we would be focusing on sub setting a vector in R in detail.

R provides us with [] operator to subset the values of a vector.

Have a look at the below syntax!

vector[start:end]

We need to provide the start index and the end index between which the subset operation is expected to perform.

Example 1

In the below example, we have created a vector of 10 data values. Further, we have performed subset operation and have extracted the first four data values using vector[1:4] operator.

rm(list = ls())
data <- 1:10

print("Vector: ")
print(data)

print("Initial 4 data values of the vector: ") 

print(data[1:4])

Output:

> print("Vector: ",data) 
[1] "Vector: "
> print(data)
 [1]  1  2  3  4  5  6  7  8  9 10
> print("Initial 4 data values of the vector: ") 
[1] "Initial 4 data values of the vector: "
> print(data[1:4])
[1] 1 2 3 4

Having said this, what if we wish to perform subset operation excluding certain values?

No worries! R has a solution for this as well.

We can exclude the data values by providing the index positions in the below format:

vector[-c(index)]

Example 2

print("Vector values excluding index 1 and 3: ") 
print(data[-c(1, 3)])

Output:

> print("Vector values excluding index 1 and 3: ") 
[1] "Vector values excluding index 1 and 3: "
> print(data[-c(1, 3)])
[1]  2  4  5  6  7  8  9  10

2. Subset of a list using [[]] operator

Apart from vectors, R provides us with various operators and functions to perform subset operation on the lists.

One such operator is [[]] operator. It enables us to subset the list and choose a particular value from the list. That is, unlike [] operator, it does not select a group of values. It rather selects and subsets a single value from the list.

Syntax:

list[[index]]

Example:

In this example, we have selected and performed subset operation on the list with the index equal to 2.

rm(list = ls())

data <- list(x = 10, y = 20, z = 30) 

print("List: \n") 
print(data) 

cat("Second element of list: ", data[[2]], "\n") 

Output:

> print("List: \n") 
[1] "List: \n"
> print(data) 
$x
[1] 10

$y
[1] 20

$z
[1] 30

> cat("Second element of list: ", data[[2]], "\n") 
Second element of list:  20

3. Subset a list using $ operator

Subset operation on R lists can even be performed using $ operator. With this operator, we subset the data values from the list based on the key of the values.

Syntax:

list$variable

Like [[]] operator, $ operator too selects only a single value as subset from the set of values.

Example:

In this example, we have performed subset operation on the list and have selected the value for the variable ‘z’.

rm(list = ls())

data <- list(x = 10, y = 20, z = 30) 

print("List: \n") 
print(data) 

cat("Third element of list: ", data$z, "\n") 

Output:

> print("List: \n") 
[1] "List: \n"
> print(data) 
$x
[1] 10

$y
[1] 20

$z
[1] 30

> cat("Second element of list: ", data$z, "\n") 
Second element of list:  30 

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, stay tuned with us!

Till then, Happy Learning! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content