R List: How to Create, Access, and Modify List Elements

Lists are the R objects which contain elements of different types like − numbers, strings, vectors, and other lists inside them. 

How to Create a List in R

To create a list in R, use the “list()” function.

To check the type in R, use the “typeof()” method.

To check the length of the List, use the “length()” method.

list_data <- list("Trump", "Biden", c(3, 11, 20), TRUE) 

print(list_data)

Output

[[1]]
[1] "Trump"

[[2]]
[1] "Biden"

[[3]]
[1] 3 11 20

[[4]]
[1] TRUE

To find the structure of List in R, you can use the str() method.

list_data <- list("Trump", "Biden", c(3, 11, 20), TRUE)
cat(str(list_data))

Output

List of 4
$ : chr "Trump"
$ : chr "Biden"
$ : num [1:3] 3 11 20
$ : logi TRUE

You can see that each element has a different data type, unlike vector.

We have created a list without a name tag, but we can create one with a name tag.

list_data <- list("X" <- "Trump", "Y" <- "Biden", 
                 "Z" <- c(3, 11, 20), "W" = TRUE)
cat(str(list_data))

Output

List of 4
$ X: chr "Trump"
$ Y: chr "Biden"
$ Z: num [1:3] 3 11 20
$ W: logi TRUE

In this example, X, Y, Z, and W are called tags, making it easier to reference the components of the list. However, tags are optional. 

Find the length of the list.

list_data <- list("Trump", "Biden", c(3, 11, 20), TRUE)

print(length(list_data))

Output

[1] 4

Find the data type of the list.

list_data <- list("Trump", "Biden", c(3, 11, 20), TRUE)

print(typeof(list_data))

Output

[1] "list"

How to Access Elements of the List

To access list elements, use vectors for indexing. The index vectors can be Integer, logical, or character.

Let’s use the character vector as an index to access the first two elements of the List.

list_data <- list("X" <- "Trump", "Y" <- "Biden", 
                 "Z" <- c(3, 11, 20), "W" <- TRUE)

print(list_data[c(1:2)])

Output

$X
[1] "Trump"

$Y
[1] "Biden"

You can use a negative integer as an index to exclude the element from the list.

list_data <- list("X" <- "Trump", "Y" <- "Biden", 
                  "Z" <- c(3, 11, 20), "W" <- TRUE)

print(list_data[-2])

Output

$X
[1] "Trump"

$Z
[1] 3 11 20

$W
[1] TRUE

The list index starts from 1. So it excludes the 2nd element from the list, as you can see from the output.

Let’s use a logical index to access list components.

list_data <- list("X" <- "Trump", "Y" <- "Biden", 
                  "Z" <- c(3, 11, 20), "W" <- TRUE)

print(list_data[c(TRUE, FALSE, TRUE, FALSE)])

Output

$X
[1] "Trump"

$Z
[1] 3 11 20

You can also access the components using the $ operator. The $ operator can do partial matching on tags.

election_date <- list("x" <- "Trump", "y" <- "Biden", 
                     "z" <- c(3, 11, 20), "w" = TRUE)

print(election_date$z)

Output

[1] 3 11 20

To reference a list member directly, we have to use the double square bracket “[[ ]]”.

How to Modify List in R

To modify a list in R, reassign the value to a specific list index.

election <- list("Trump", "Biden", c(3, 11, 20), TRUE)
print(election)
print("After changing the 2nd element")
election[2] <- "Kamala"
print(election)

Output

[[1]]
[1] "Trump"

[[2]]
[1] "Biden"

[[3]]
[1] 3 11 20

[[4]]
[1] TRUE

[1] "After changing the 2nd element"
[[1]]
[1] "Trump"

[[2]]
[1] "Kamala"

[[3]]
[1] 3 11 20

[[4]]
[1] TRUE

You can see that the 2nd element of the list is changed from Biden to Kamala.

How to Delete Element from the List in R

To remove the component in R, assign NULL to that component.

election <- list("Trump", "Biden", c(3, 11, 20), TRUE)
print(election)
print("After removing the 3rd element")
election[3] <- NULL
print(election)

Output

[[1]]
[1] "Trump"

[[2]]
[1] "Biden"

[[3]]
[1] 3 11 20

[[4]]
[1] TRUE

[1] "After removing the 3rd element"
[[1]]
[1] "Trump"

[[2]]
[1] "Biden"

[[3]]
[1] TRUE

You can see that the third element is removed, and now the fourth element becomes the third element.

That’s it.

Leave a Comment