Lists in R Programming

Filed Under: R Programming
Lists In R

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.

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.

alist <-list(name="Ram",age=25,salary=20000)

Each component of the list can be accessed by its name, preceded by a dollar sign.

> alist$name
[1] "Ram"
> alist$age
[1] 25
> alist$salary
[1] 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[[1]]
[1] "Ram"
> alist[[2]]
[1] 25
> alist[[3]]
[1] 20000

Indexing using names of the components within the square braces is also allowed.

> alist <-list(name="Ram",age=25,salary=20000)
> alist[['name']]
[1] "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[2]
[1] "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]
[1] 20

Assigning Names

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
[1] "Right" "Left"  "Jump"  "Duck" 

$Positions
     [,1] [,2]
[1,] 22.4   20
[2,] 32.5   28

$Name
[1] "Mario"

Modifying lists

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
[1] "Right" "Left"  "Jump"  "Duck" 

$Positions
     [,1] [,2]
[1,] 22.4   20
[2,] 32.5   28

$Name
[1] "Mario"

$lives
[1] 3

Nesting Lists

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
[1] "Right" "Left"  "Jump"  "Duck" 

$Positions
     [,1] [,2]
[1,] 22.4   20
[2,] 32.5   28

$Name
[1] "Mario"

$lives
[1] 3

$player
$player$name
[1] "Adam"

$player$age
[1] 16

$player$location
[1] "USA"

When we need to refer to the location of the player within the mylist, we do it using mylist$player$location.

> mylist$player$location
[1] "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.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages