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

Heinz Tuechler tuechler at gmx.at
Mon Sep 28 16:58:11 CEST 2009


Hadley,

many thanks for your answer and for the enormous work you put into 
plyr, a really powerful package.
For now, I will solve my problem with a variable label attribute, I 
usually attach to columns in data frames. I asked the list, because I 
thought, I am overlooking something trivial, since lapply itself 
apparently "knows" the object names, as it labels the output by them. 
It just does not supply them to the function it calls.
Maybe deparse(substitute(x)) with the right environment would do it, 
but I did not find it.

Thanks,
Heinz


At 16:27 28.09.2009, hadley wickham wrote:
> > or with l_ply (plyr package)
> > l_ply(data.frame(a=1:3, b=2:4), function(x) print(deparse(substitute(x))))
>
>
>The best way to do this is to supply both the object you want to
>iterate over, and its names.  Unfortunately it's slightly difficult to
>create a data structure of the correct form to do this with m_ply.
>
>df <- data.frame(a=1:3, b=2:4)
>input <- list(x = df, name = names(df))
>inputdf <- structure(input,
>     class = "data.frame",
>     row.names = seq_along(input[[1]]))
>
>m_ply(inputdf, function(x, name) {
>   cat(name, "---------\n")
>   print(x)
>})
>
>I'll think about how to improve this for a future version.
>
>Hadley
>
>
>--
>http://had.co.nz/




More information about the R-help mailing list