The R documentation (as well as extensive R folklore) describes the different operators for extraction.
However, it appears that neither the documentation nor the folklore explain why it is that the use of different operators ( [ versus [[ ) can return identical results. Can someone provide an explanation? Note that I'm asking not why they can produce different results, but why they can sometimes produce an identical result. Why does x[2] (see below) evaluate identically to x[[2]]?
[
[[
x[2]
x[[2]]
x <- c("a","b","c") ; x[2] ; x[[2]] ## [1] "b" ## [1] "b"
There is no distinction in R between a value (e.g. 3) and a vector of length 1 (e.g. c(3)). x[[2]] should give you a value and x[2] should give you a vector of length 1. But these two things are identical.
3
c(3)
1.4m articles
1.4m replys
5 comments
57.0k users