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

List in R is a data structure having elements of different data types. A Vector contains all elements of the same type is called an atomic vector, and a vector containing all elements of a different type is called List in R.

## R List

A list is a particular type of vector but can store mixed types. A list can contain different types of elements like − numbers, strings, vectors, another list, a matrix, or even a function as its elements.

## 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

`````` Rscript Pro.R
[]
 "Trump"

[]
 "Biden"

[]
 3 11 20

[]
 TRUE
``````

To find the structure of List in R, 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 a list 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

`` 4``

Find the data type of the list.

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

#### Output

`` "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 List.

``````list_data <- list("X" <- "Trump", "Y" <- "Biden", "Z" <- c(3, 11, 20), "W" <- TRUE)
print(list_data[c(1:2)])
``````

#### Output

``````\$X
 "Trump"

\$Y
 "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
 "Trump"

\$Z
 3 11 20

\$W
 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
 "Trump"

\$Z
 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

`` 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 index of the list.

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

#### Output

``````[]
 "Trump"

[]
 "Biden"

[]
 3 11 20

[]
 TRUE

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

[]
 "Kamala"

[]
 3 11 20

[]
 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 <- NULL
print(election)``````

#### Output

``````[]
 "Trump"

[]
 "Biden"

[]
 3 11 20

[]
 TRUE

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

[]
 "Biden"

[]
 TRUE``````

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

## How to Convert R List to Vector

To convert List to Vector in R, use the unlist() function. The unlist() function takes the list as input and produces a vector. All the arithmetic operations on vectors can be applied after the list is converted into vector.

``````data_list <- list(1:5)
cat(str(data_list))

cat("After converting from List to Vector", "\n")

vec <- unlist(data_list)
print(vec)``````

#### Output

``````List of 1
\$ : int [1:5] 1 2 3 4 5
After converting from List to Vector
 1 2 3 4 5``````

You can see that using the unlist() method, and the list is converted into a vector.

## How to Merge Lists in R

To merge the lists in R, by placing all the lists element inside one c() function.

``````num_list <- list("x", TRUE, 11)
data_list <- list("y", FALSE, 21)

com_list <- c(num_list, data_list)
print(com_list)``````

#### Output

``````[]
 "x"

[]
 TRUE

[]
 11

[]
 "y"

[]
 FALSE

[]
 21
``````

You can see from the output that the c() function is used to merge the lists.

## How to Generate Lists in R

To generate a list, use the colon operator ( : ) to generate a list of numbers.

``````num_list <- 1:6
print(num_list)``````

#### Output

`` 1 2 3 4 5 6``

## R Predefined Lists

There are many predefined lists available like letters, and month names are predefined.

``print(month.abb)``

#### Output

`` "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"``

## Conclusion

A list is a generic vector containing other objects. In this tutorial, we have seen how to create, add, access, update, and remove list items. You can convert the list to vector using the unlist() method, and there are many inbuilt lists out there in R.

Categories R