Assume that you have a list and you want to get the n-th element of each component or generally to subset the list. You can use the command sapply(list, "[", c(1,2,3,..))
Let’s see this in practice.
mylist<-list(id = 1:10, gender = c("m","m","m","f","f","f","m","f","f","f"), amt = c(5,20,30,10,20,50,5,20,10,30) ) mylist
Output:
$id
[1] 1 2 3 4 5 6 7 8 9 10
$gender
[1] "m" "m" "m" "f" "f" "f" "m" "f" "f" "f"
$amt
[1] 5 20 30 10 20 50 5 20 10 30
Let’s say that we want to get the 3rd and 6th element of the list:
sapply(mylist, "[", c(3,6))
Output:
id gender amt
[1,] "3" "m" "30"
[2,] "6" "f" "50"
Notice:
As a friend mentioned below, the issue with the sapply
and the matrix is that it expects the data type everywhere and it converts everything to a character since the “gender” is a character variable.
Another approach will be to use the lapply
:
lapply(mylist, "[", c(3,6))
Output:
$id
[1] 3 6
$gender
[1] "m" "f"
$amt
[1] 30 50
Now, if we want the results to appear as data.frame
data.frame(lapply(mylist, "[", c(3,6)))
Output:
id gender amt
1 3 m 30
2 6 f 50
4 thoughts on “Hack: The ‘[‘ in R lists”
And for those, who feel that “[” is not a good name for a function, magrittr package comes with so called aliases.
An alias for [ is extract, so you may use:
library(magrittr)
sapply(mylist, extract, c(3,6))
to get the same result.
thank you
One possible drawback to using sapply() in this case is that the result is “simplified” into a matrix and everything is converted to character since a matrix can only contain one data type. Using lapply() will preserve the type:
> lapply(mylist, “[“, c(3,6))
[[1]]
[1] 3 6
[[2]]
[1] “m” “f”
[[3]]
[1] 30 50
Also, to include names in your list, you need to use “=” instead of ” mylist sapply(mylist, “[“, c(3,6))
id gender amt
[1,] “3” “m” “30”
[2,] “6” “f” “50”
> lapply(mylist, “[“, c(3,6))
$id
[1] 3 6
$gender
[1] “m” “f”
$amt
[1] 30 50
> as.data.frame(lapply(mylist, “[“, c(3,6)), col.names = names(mylist))
id gender amt
1 3 m 30
2 6 f 50
Nice feedback. Thank you!