-4

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
smci
  • 32,567
  • 20
  • 113
  • 146
  • 2
    Isn't this a duplicate of [The difference between bracket and double bracket for accessing the elements of a list or dataframe](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-elem)? – Maurits Evers Apr 26 '19 at 01:12
  • Hi MauritsEvers , wasn't aware of that one - I just spent 40 min searching for any other canonical and failing to find one, so I wrote this one, to help [this person](https://stackoverflow.com/questions/52086833/using-the-nth-element-from-a-list-as-an-input-for-a-function) who was complaining that getting the nth element of a list "doesn't work", and noone corrected that. I suggest this title and wording are more prescriptive and hence better for new users and non-R-programmers. But you and the community may close whichever as duplicate. I think this title as dupe plugs a major gap. – smci Apr 26 '19 at 01:17
  • 1
    Hi @smci; not sure on whether this is easier to find/digest than the dupe target but others can decide on that. From what I can tell, the linked post is actually one of the top voted posts here on SO with an R tag. For what it's worth, I didn't downvote your question. – Maurits Evers Apr 26 '19 at 01:20
  • 1
    ...also for findability (both from SO search, and inbound from Google), punctuation like `[n]` and `[[n]]` will not be found, will not be understood as synonyms for 'bracket' and 'double bracket', and the term 'accessing' will be distinct to 'indexing' (or the geeky 'extracting'). So, whichever way we go on closing which one, helpful to improve the abysmal SEO coverage, since new users clearly aren't finding the answer to this basic question. – smci Apr 26 '19 at 01:21
  • 1
    Hmm, as I said, I don't think I agree with *"new users clearly aren't finding the answer to this basic question"*. The dupe target question has 454 upvotes and the top answer 282 votes. – Maurits Evers Apr 26 '19 at 01:23
  • @MauritsEvers: a) as I illustrated [this question complained that getting the nth element of a list "doesn't work"](https://stackoverflow.com/questions/52086833/using-the-nth-element-from-a-list-as-an-input-for-a-function), and noone corrected that in a year. b) I have used SO for 9 years on a near-daily-basis and did not recollect the other answer existed, nor did I find it using SO search for 40 min just now. That's a fail. c) As I just explained, SO's search coverage won't understand the equivalence of [n] and [[n]] to 'bracket'/'double bracket', nor 'accessing'/ 'indexing'/ 'extracting'. – smci Apr 26 '19 at 01:27
  • tl;dr: More coverage is better, nay essential. The community can close-as-dupe whichever they want. Either way, this question title and wording bring improvement (e.g. improve synonymy on SO internal search). – smci Apr 26 '19 at 01:30
  • 2
    That question has around ~200.000 views, so definitely people are getting there ;} I don't think it makes sense at all to close that question and put this one as a duplicate. Beginners have a specific lingo/way of searching that an experienced person don't. You may try to replicate, but its not assured to work. That question has proved it works – rafaelc Apr 26 '19 at 01:33
  • @smci I'm not here to argue with you, and as I said I won't hammer-close your post but think that the community should decide. Personally I think this is a dupe that doesn't add anything new (your answer is almost identical to the top answer of the dupe target); I completely agree with RafaelC's comment in that closing the original post makes no sense. – Maurits Evers Apr 26 '19 at 01:39
  • @RafaelC: No, not at all. a) I have used SO for 9 years on a near-daily-basis and did not recollect the other answer, nor can it be found via SO search. b) You cannot find it via SO search, or the close-as-dupe search box. b2) Punctuation symbols are known to be search-engine-proof c) If I'm searching for "indexing into a list with [[n]]", I wouldn't dream of typing "accessing a list with double-bracket". d) Viewcounts prove nothing, they do not show how many thousand users could not find that e) There is a genuine search engine coverage problem here, which I'm helping fix. – smci Apr 26 '19 at 01:39
  • @MauritsEvers: do you understand what I'm saying both about prescriptiveness and search engine coverage? A non-R-user will simply use `[n]` as they would in any other language, and conclude "R indexing doesn't work on lists". They would not think to search for "accessing using double-brackets" (neither will SO search or Google), and even if someone did manage to cite them the link, it doesn't seem to be the answer to their problem. Whereas "Why is indexing into a list with list [n] instead of list[[n]] in R not what you would expect?" is **incapable of being misunderstood, it's prescriptive**. – smci Apr 26 '19 at 01:43
  • @MauritsEvers: Fine. I'm not rephounding, I was genuinely unaware of the other question (and so are the search engines). Anyway there's a compelling argument that in general an older, better-known, more-upvoted asking should be kept as primary, even where the title, wording and tags are worse or less amenable to being found. Close as y'all wish, either way the coverage improves... – smci Apr 26 '19 at 01:46
  • @smci Yes I understand the point you're trying to make. But I can't say that your question title is any more prescriptive and/or less likely to being misunderstood than the original dupe target. Especially for a new user. General indexing with brackets is probably one of the first things a new R user does; incorporating this into a meaningful SO/Google search is then but a small step away IMO. Let's agree to disagree; we both make assumptions on what new users would (or would not) do. – Maurits Evers Apr 26 '19 at 01:47

1 Answers1

0

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(!)

smci
  • 32,567
  • 20
  • 113
  • 146