Search code examples
rlistsubsetsapply

What documentation tells us that "[" can be used as a function?


Several of the answers to this question suggest that the best way to extract a subset from a list is to use something like sapply(mylist, "[", y). I found this rather disturbing, because I have never seen anything to suggest that we can use "[" as if it's a function. Where is this documented? I checked ?'[[' for my version, 3.6.3, but I can't see any reference to this functionality in that documentation.


Solution

  • All operators in R are functions. This is documented in the language definition:

    https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Operators

    Like the other operators, indexing is really done by functions, and one could have used `[`(x, 2) instead of x[2].