Lists in R can group together different kinds of variables into a single compound structure. For this reason, lists prove very useful in handling data. A list can contain a numeric, integer string, factor or even another list as its component.
Table of Contents
Creating Lists in R
Lists in R can be thought of as similar to vectors in R. However, vectors are more or less atomic since their components cannot be broken down further.
Lists are often known as recursive vectors, as they are comprised of complex structures like vectors and other lists that may have more components.
Let us create a list first.
Each component of the list can be accessed by its name, preceded by a dollar sign.
> alist$name  "Ram" > alist$age  25 > alist$salary  20000
Indexing Lists in R
However, it is not always necessary to name the components of a list. We can also refer to the elements using basic indexing. Let us create the same list without the component names.
> alist <-list("Ram",25,20000)
Now we can access each of these elements within the list. Notice how the indexing starts from 1 compared to the other programming languages where the first element is usually 0.
> alist[]  "Ram" > alist[]  25 > alist[]  20000
Indexing using names of the components within the square braces is also allowed.
> alist <-list(name="Ram",age=25,salary=20000) > alist[['name']]  "Ram"
Let us look at more complex lists and how we index them. Consider we are building a game where a character gets a choice at each position. All the information about the gameplay is encapsulated in a list as follows.
> mylist <-list(choices=c("Right","Left","Jump","Duck"),pos=matrix(c(22.4,32.5,20,28),ncol=2,nrow=2),charname="Mario")
The list here contains a vector for the choices available, the matrix for the character’s position on screen and the name of the character as a string. Let us see how we index these.
#Access the elements of the vector within the list > mylist$choices  "Left" #Get the matrix from the list > mylist$pos [,1] [,2] [1,] 22.4 20 [2,] 32.5 28 #Access elements of matrix using the indexes > mylist$pos[1,2]  20
You can also reassign the names for the components of a list after you created it as follows. This method works whether you have already assigned the names or not.
> names(mylist)=c("Actions","Positions","Name") > mylist $Actions  "Right" "Left" "Jump" "Duck" $Positions [,1] [,2] [1,] 22.4 20 [2,] 32.5 28 $Name  "Mario"
You can always add new components to the list using the dollar sign as follows. We’ll demonstrate the addition of a new component named “lives” with an integer value of 3.
> mylist$lives <-3 > mylist $Actions  "Right" "Left" "Jump" "Duck" $Positions [,1] [,2] [1,] 22.4 20 [2,] 32.5 28 $Name  "Mario" $lives  3
A list can be added as a component to another list. This can be done to any depth level. The indexing in such cases follows the pattern of nesting. Let us look at an example.
#Create a list to represent the player > player <- list(name="Adam",age=16,location= "USA") #Add this as a component to the list we created earlier. > mylist$player <- player #Display the list. > mylist $Actions  "Right" "Left" "Jump" "Duck" $Positions [,1] [,2] [1,] 22.4 20 [2,] 32.5 28 $Name  "Mario" $lives  3 $player $player$name  "Adam" $player$age  16 $player$location  "USA"
When we need to refer to the location of the player within the
mylist, we do it using
> mylist$player$location  "USA"
It is important to bear the nesting order in mind when trying to access the list elements in the above manner. Lists are usually employed to return function outputs. However, these can consume a lot of memory compared to vectors and matrices due to their variability. Therefore, it is necessary to tackle lists with care while programming.