Lists are a multi functionaly tool. They have hace different data types, lists of lists, names and more. In this article, we will see how to select items from a list in R.
The first way to select a list is by position. We use the
[] double brackets operator. This may be confusing at first if you know a different language, but the double brackets is a generic tool in R.
temps = list(45, 67, 77) temps[] # selects the first item at pos 1
We can also pass a vector of positions to select multiple items.
temps = list(45, 67, 77) temps[c(1, 3)] # selects first and third item
If we have a named list, we can use similar sytax to select item by name.
temps = list(mon=45, tues=67, wed=77) temps[["tues"]] # selects the item associated with Tues
Again, we can pass a vector of names to select multiple items.
temps = list(mon=45, tues=67, wed=77) temps[c("mon", "wed")] # Select mon and wed