Understanding Lists in R

Lists are yet another way to store and retrieve information in R. The advantage of lists are their high degree of flexibility. You can store almost anything in any combination in a list in ways that you could never store information in a vector, matrix, or data frame. This post will provide an introduction to ways to develop lists.

Making a List 

Making a list involves the use of the ‘list’ function. Our first list will involve converting a matrix to a list with the additional information of the year of the list.

> points.team
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22
> team.list <-list(points.team, '2014-15')
> team.list
[[1]]
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22

[[2]]
[1] "2014-15"

Here is what we did

  1. We printed the matrix ‘points.team’ If you forgot how to create this please CLICK HERE
  2. We then create the variable ‘team.list’ and assign two elements to this list
    1. The matrix ‘points.team’
    2. The year 2014-2015
  3. Next, we display team.list

The number in double brackets [ [ ] ] represent the element number. The ‘points.team’ is the first element in the list. The number in single brackets [ ] represent a sub-element of an element. For example, ‘2014-15’ is a sub-element of the unnamed element 2 in the ‘team.list’ list.

Making Named List

You can name each element in a list. Below is an example using the same data.

> Named.List<- list(scores=points.team, season='2014-15')
> Named.List
$scores
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22

$season
[1] "2014-15"

Here is what happened

  1. We created the variable ‘Named.List’ and assigned scores as the data from ‘points.team’ and season with the information ‘2014-15
  2. We display the list
  3. If you look closely instead of seeing double brackets [ [ ] ] you should see instead the name $scores and $season in the list.

All the tricks below apply to data frames as well

Extracting Elements from a List

Let’s say you want to extract the ‘points.team’ information from the ‘Named.List’ here is how to do it.

> Named.List[['scores']]
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22

All you do is simply type the name of the element in double brackets after the name of the list

Changing Information in a List

Let’s say that you want to change the season year from ‘2014-2015’ to 2013-14. Here is one way to do this

> Named.List[2] <- list('2013-2014')
> Named.List
$scores
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22

$season
[1] "2013-2014"

All we did was type the name of the variable ‘Named.List’ use the brackets to indicate the second element and assigned ‘2013-2014’ using the ‘list’ function.

Adding Information to a List

You now want to add the names of the opponents that James and Kevin faced over the six games. To do this examine the following.

> Opponents<- c("Warriors", "Kings","Hawks","Wizards","Bulls","Grizzlies")
> Named.List<- list(scores=points.team, season='2014-15', Opponents=Opponents)
> Named.List
$scores
      1st 2nd 3rd 4th 5th 6th
James  12  15  30  25  23  32
Kevin  20  19  25  30  31  22

$season
[1] "2014-15"

$Opponents
[1] "Warriors"  "Kings"     "Hawks"     "Wizards"   "Bulls"     "Grizzlies"

Here is what we did

  1. You created a variable called ‘Opponents’ and entered the name of six teams
  2. Next, you type ‘Named.List’ and you add the previous information along with the new argument Opponents = Opponents. This tells R to add your variable ‘Opponents’ to the list.
  3. Finally, we display the modified list.

Conclusion

There is a lot of overlap in what you can do with lists, data frames, and matrices. Therefore, keep in mind that much that could be done with this other objects can be done with lists as well. The benefit of lists are the ability to combine so much diverse information in one place.

5 thoughts on “Understanding Lists in R

  1. Pingback: Understanding Lists in R | Education and Resear...

  2. rogedumak

    Dr. Darrin, I can not hold back my appreciation to you and your sponsor again and again. Your ideas are easy for me to capture and understand, especially , the statistics. I would like you discuss about population distribution , begin with common normal , chi square etc

    Reply
  3. Pingback: Apply Functions in R | educationalresearchtechniques

  4. Pingback: Calculating Chi-Square in R | educationalresearchtechniques

Leave a Reply