In R the
[[ ]] is the operator that (when supplied a scalar argument) pulls a single element out of lists (and the
[ ] operator pulls out sub-lists).
[[ ]] and
[ ] appear to be synonyms. However, when writing reusable code you may not always be sure if your code is going to be applied to a vector or list in the future. It is safer to get into the habit of always using
[[ ]] when you intend to retrieve a single element.
Example with lists:
list("a", "b") #> [] #>  "a" list("a", "b")[] #>  "a"
Example with vectors:
c("a", "b") #>  "a" c("a", "b")[] #>  "a"
Note on this article series.
This R tips series is short simple notes on R best practices, and additional packaged tools. The intent is to show both how to perform common tasks, and how to avoid common pitfalls. I hope to share about 20 of these about every other day to learn from the community which issues resonate and to also introduce some of features from some of our packages. It is an opinionated series and will sometimes touch on coding style, and also try to showcase appropriate Win-Vector LLC R tools.