Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does '[[' mean in the function lapply(x, '[[', VarNames[[type]]) in R?

Tags:

r

Can anyone tell me what [[ means in the function lapply(x, '[[', VarNames[[type]]) in R?

like image 469
user1787675 Avatar asked Oct 31 '12 06:10

user1787675


3 Answers

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"
like image 95
thelatemail Avatar answered Oct 17 '22 22:10

thelatemail


It is the function [[ which extracts single elements. See ?"[["

It is the same function you see at work in

VarNames[[type]]   
like image 31
mnel Avatar answered Oct 17 '22 21:10

mnel


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.

like image 2
IRTFM Avatar answered Oct 17 '22 21:10

IRTFM