How to Create a List in R

To create a list in R, you can use the “list()” function. It accepts single or multiple arguments and returns the list.

Creating a list using list() function

lt <- list("SBF", "Alameda")

print(lt)

Output

[[1]]
[1] "SBF"

[[2]]
[1] "Alameda"

You can see that we created a list with two elements.

To print the data in the console, use the print() function.

Creating a list with different data types

lt <- list("SBF", TRUE, 19, 21.19, c(19, 21))

print(lt)

Output

[[1]]
[1] "SBF"

[[2]]
[1] TRUE

[[3]]
[1] 19

[[4]]
[1] 21.19

[[5]]
[1] 19 21

We created a list filled with multiple data type elements.

Further reading

Creating an empty list

Append an element to an empty list

Leave a Comment