Is there a way to get the list index name in my lapply() function?
n = names(mylist)
lapply(mylist, function(list.elem) { cat(\"What is the name of this list
UPDATE for R version 3.2
Disclaimer: this is a hacky trick, and may stop working in the the next releases.
You can get the index using this:
> lapply(list(a=10,b=20), function(x){parent.frame()$i[]})
$a
[1] 1
$b
[1] 2
Note: the [] is required for this to work, as it tricks R into thinking that the symbol i (residing in the evaluation frame of lapply) may have more references, thus activating the lazy duplication of it. Without it, R will not keep separated copies of i:
> lapply(list(a=10,b=20), function(x){parent.frame()$i})
$a
[1] 2
$b
[1] 2
Other exotic tricks can be used, like function(x){parent.frame()$i+0} or function(x){--parent.frame()$i}.
Performance Impact
Will the forced duplication cause performance loss? Yes! here are the benchmarks:
> x <- as.list(seq_len(1e6))
> system.time( y <- lapply(x, function(x){parent.frame()$i[]}) )
user system elapsed
2.38 0.00 2.37
> system.time( y <- lapply(x, function(x){parent.frame()$i[]}) )
user system elapsed
2.45 0.00 2.45
> system.time( y <- lapply(x, function(x){parent.frame()$i[]}) )
user system elapsed
2.41 0.00 2.41
> y[[2]]
[1] 2
> system.time( y <- lapply(x, function(x){parent.frame()$i}) )
user system elapsed
1.92 0.00 1.93
> system.time( y <- lapply(x, function(x){parent.frame()$i}) )
user system elapsed
2.07 0.00 2.09
> system.time( y <- lapply(x, function(x){parent.frame()$i}) )
user system elapsed
1.89 0.00 1.89
> y[[2]]
[1] 1000000
Conclusion
This answer just shows that you should NOT use this... Not only your code will be more readable if you find another solution like Tommy's above, and more compatible with future releases, you also risk losing the optimizations the core team has worked hard to develop!
Old versions' tricks, no longer working:
> lapply(list(a=10,b=10,c=10), function(x)substitute(x)[[3]])
Result:
$a
[1] 1
$b
[1] 2
$c
[1] 3
Explanation: lapply creates calls of the form FUN(X[[1L]], ...), FUN(X[[2L]], ...) etc. So the argument it passes is X[[i]] where i is the current index in the loop. If we get this before it's evaluated (i.e., if we use substitute), we get the unevaluated expression X[[i]]. This is a call to [[ function, with arguments X (a symbol) and i (an integer). So substitute(x)[[3]] returns precisely this integer.
Having the index, you can access the names trivially, if you save it first like this:
L <- list(a=10,b=10,c=10)
n <- names(L)
lapply(L, function(x)n[substitute(x)[[3]]])
Result:
$a
[1] "a"
$b
[1] "b"
$c
[1] "c"
Or using this second trick: :-)
lapply(list(a=10,b=10,c=10), function(x)names(eval(sys.call(1)[[2]]))[substitute(x)[[3]]])
(result is the same).
Explanation 2: sys.call(1) returns lapply(...), so that sys.call(1)[[2]] is the expression used as list argument to lapply. Passing this to eval creates a legitimate object that names can access. Tricky, but it works.
Bonus: a second way to get the names:
lapply(list(a=10,b=10,c=10), function(x)eval.parent(quote(names(X)))[substitute(x)[[3]]])
Note that X is a valid object in the parent frame of FUN, and references the list argument of lapply, so we can get to it with eval.parent.