[R] Using '[' as a function

chipmaney chipmaney at hotmail.com
Fri Jul 30 00:18:25 CEST 2010


I am learning R, and instead of learning by rote, I am trying to better
understand the language in order to improve my programming. So any
"meta-information" on why the following code works would be greatly
appreciated...

I obtained this code to extract the first record from each of a series of
vectors in a list:

>example<- list(c(1,2),c(3,4),c(4,5))

[[1]]
[1] 1 2

[[2]]
[1] 3 4

[[3]]
[1] 4 5

>sapply(example,'[',1)

[1] 1 3 4

however, after perusing my book and the interweb, i remain puzzled about how
'[' works as a function in sapply. 

-Why does R recognize '[' as a function?  
-Why does it need the quotes?
- How does the function know to use the optional(?) argument "1" as the
index location?
- Any other information linking this specific example to the broader R
environment?

Any explanation of how this function works will be a small incremental gain
in my understanding of R, so thanks in advance.

Chipper

-- 
View this message in context: http://r.789695.n4.nabble.com/Using-as-a-function-tp2307292p2307292.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list