I try to read out certain elements from a list in a way, thats equivalent to df[, c(1,4,5)]
in a data.frame
.
> obj <- list(c(1:5)
You should call lapply with a function which is run on every list entry:
obj <- list(c(1:5), c(1:5))
lapply(obj, function(x) x[c(1, 4, 5)])
#[[1]]
[1] 1 4 5
[[2]]
[1] 1 4 5
EDi has a great answer, but you can do it by passing the [
function to lapply
plus additional arguments:
lapply(obj, '[', c(1, 4, 5))
You can access this and the other "weird" functions in R by quoting them:
?"["