Can anyone tell me what [[
means in the function lapply(x, '[[', VarNames[[type]])
in R?
It's an extraction function. As @mnel notes, the help file at ?Extract
will give you lots of information.
Here are a couple of examples using [[
and [
as functions as you would more normal looking base functions like sum
table
etc:
> test <- list(a=1:10,b=letters[1:10])
> test
$a
[1] 1 2 3 4 5 6 7 8 9 10
$b
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> "[["(test,1)
[1] 1 2 3 4 5 6 7 8 9 10
> "[["(test,2)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> "["(test,1)
$a
[1] 1 2 3 4 5 6 7 8 9 10
> "["(test,2)
$b
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
It is the function [[ which extracts single elements. See ?"[["
It is the same function you see at work in
VarNames[[type]]
That expression will cause each successive value of 'x' to be given to [[
as its first argument and for VarNames[[type]]
to be evaluated and used as the second argument. The result should be a series of function calls of the form:
`[[`( x[[1]], VarNames[[type]] )
Notice I presented this as a functional form. The usual way of seeing this written for a first single case would be :
x[[1]][[ VarNames[[type]]) ]]
That second form gets parsed into the first form by the R interpreter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With