This question already has an answer here:
In an R list, why does indexing with [n]
instead of [[n]]
not return the nth element as non-R-programmers would expect?
lfile <- list('fileA.xls', 'file2.xls', 'fileY.xls')
ll <- list(list(1,2), list(3), list(4,5,6), list(7,8))
lv <- list(c(1,2), c(3), c(4,5,6), c(7,8))
> lfile[2]
[[1]]
[1] "file2.xls" # returns SUBLIST, not n'th ELEMENT
> lfile[[2]]
[1] "file2.xls" # returns ELEMENT
Because in general
l[n]
returns a sublist (possibly of length one), whereasl[[n]]
returns an element:From R intro manual: 6.1 Lists:
This is an R gotcha (R being different to other languages in a non-obvious and under-documented way), although as ever some R users will insist it's doing exactly what it says, (somewhere deep and unindexed) in the doc. However, the help-page for list only shows you how to create a list but does not show how to index into it(!) Only ?
[
or?Extract
manpages actually tell you how to index into a list(!)