Lists are a powerful data structure in R. At the basic level, they extend the functionality of vectors by allowing you to create groups of different data types. In this article, we will see how to create lists in R.
To create a list, we have a few main ways. First, we can use the list
method and pass a number of variables to it.
group = list(3, "test", c(1, 2, 3))
group
Notice, that our list can have many different data types, unlike a vector.
We can also, create a list, then add to the list later. To add to a index, we need to use the [[]]
double bracket operator. Those from other languages may find this confusing, but the double brackets is a generic tool in R.
group = list()
group[[1]] = 3
group[[2]] = "test"
group[[3]] = c(1, 2, 3)
group
Above, we create the same list, but by adding the item after initialization.
Finally, we can also create a list with names. This is similar to dictionaries or hash maps in other lanugaegs.
group = list(count = 3, name = "test", details = c(1, 2, 3))
group