How to Change the Components in Lists in R

By Andrie de Vries, Joris Meys

Much like many other objects that you will encounter in R, lists aren’t static objects. You can change components, add components, and remove components from them in a pretty straightforward manner.

Changing the value of components

Assigning a new value to a component in a list is pretty straightforward. You use either the $ or the [[ ]] to access that component, and simply assign a new value. If you want to replace the scores in the list baskets.nlist with the data frame baskets.df, for example, you can use any of the following options:

> baskets.nlist[[1]] <- baskets.df
> baskets.nlist[[“scores”]] <- baskets.df
> baskets.nlist$scores <- baskets.df

If you use [ ], the story is a bit different. You can change components using [ ] as well, but you have to assign a list of components. So, to do the same as the preceding options using [ ], you need to use following code:

> baskets.nlist[1] <- list(baskets.df)

All these options have exactly the same result, so you may wonder why you would ever use the last option. Simple: Using [ ] allows you to change more than one component at once. You can change both the season and the scores in baskets.list with the following line of code:

> baskets.list[1:2] <- list(baskets.df, “2009-2010”)

This line replaces the first component in baskets.list with the value of baskets.df, and the second component of baskets.list with the character value 2009-2010.

Removing components

Removing components is even simpler: Just assign the NULL value to the component. In most cases, the component is simply removed. To remove the first component from baskets.nlist, you can use any of these (and more) options:

> baskets.nlist[[1]] <- NULL
> baskets.nlist$scores <- NULL
> baskets.nlist[“scores”] <- NULL

Using single brackets, you again have the possibility of deleting more than one component at once. Note that, in this case, you don’t have to create a list with the value NULL first. To the contrary, if you were to do so, you would give the component the value NULL instead of removing it, as shown in the following example:

> baskets.nlist <- list(scores = baskets.df, season = “2010-2011”)
> baskets.nlist[“scores”] <- list(NULL)
> baskets.nlist
[1] “2010-2011”

Adding extra components using indices

You can use either the $ or indices to add extra variables. Lists work the same way; to add a component called players to the list baskets.nlist, you can use any of the following options:

> baskets.nlist$players <- c(“Granny”, “Geraldine”)
> baskets.nlist[[“players”]] <- c(“Granny”, “Geraldine”)
> baskets.nlist[“players”] <- list(c(“Granny”, “Geraldine”))

Likewise, to add the same information as a third component to the list baskets.list, you can use any of the following options:

> baskets.list[[3]] <- c(“Granny”, “Geraldine”)
> baskets.list[3] <- list(c(“Granny”, “Geraldine”))

These last options require you to know exactly how many components a list has before adding an extra component. If baskets.list contained three components already, you would overwrite that one instead of adding a new one.

Combining lists

If you wanted to add components to a list, it would be nice if you could do so without having to worry about the indices at all. For that, the only thing you need is a function you use extensively, the c() function.

That’s right, the c() function — which is short for concatenate — does a lot more than just creating vectors from a set of values. The c() function can combine different types of objects and, thus, can be used to combine lists into a new list as well.

In order to be able to add the information about the players, you have to create a list first. To make sure you have the same output, you have to rebuild the original baskets.list as well. You can do both using the following code:

> baskets.list <- list(, “2010-2011”)
> players <- list(rownames(

Then you can combine this players list with the list goal.list like this:

> c(baskets.list, players)
     1st 2nd 3rd 4th 5th 6th
Granny   12  4  5  6  9  3
Geraldine  5  4  2  4 12  9
[1] “2010-2011”
[1] “Granny”  “Geraldine”

If any of the lists contains names, these names are preserved in the new object as well.