[R] How to assess object names within a function in lapply or l_ply?

Heinz Tuechler tuechler at gmx.at
Mon Sep 28 13:22:30 CEST 2009


Dear All,

to produce output of several columns of a data frame, I tried to use 
lapply and also l_ply. In both cases, I would like to print a header 
line containing also the name of the respective column in the data frame.

For example, I would like the following

lapply(data.frame(a=1:3, b=2:4), function(x) print(deparse(substitute(x))))

to produce:
[1] "a"
[1] "b"

and not, what it actually does:
[1] "X[[1L]]"
[1] "X[[2L]]"
$a
[1] "X[[1L]]"

$b
[1] "X[[2L]]"

or with l_ply (plyr package)
l_ply(data.frame(a=1:3, b=2:4), function(x) print(deparse(substitute(x))))

to produce:
[1] "a"
[1] "b"

and not, what it actually does:
[1] ".data[[i]]"
[1] ".data[[i]]"

Is this possible?

Thanks,
Heinz




More information about the R-help mailing list