Because in general l[n]
returns a sublist (possibly of length one), whereas l[[n]]
returns an element:
> lfile[2]
[[1]]
[1] "file2.xls" # returns SUBLIST of length one, not n'th ELEMENT
> lfile[[2]]
[1] "file2.xls" # returns ELEMENT
From R intro manual: 6.1 Lists:
It is very important to distinguish Lst[[1]] from Lst[1].
‘[[…]]’ is the operator used to select a single element,
whereas ‘[…]’ is a general subscripting operator.
Thus the former is the first object in the list Lst, and if it is a named list the name is not included. The latter is a sublist of the list Lst consisting of the first entry only. If it is a named list, the names are transferred to the sublist.
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(!)
solved Why does indexing into a list with list[n] instead of list[[n]] in R not do what you would expect? [duplicate]