[R] How to assess object names within a function in lapply or l_ply?
Henrique Dallazuanna
wwwhsd at gmail.com
Mon Sep 28 13:57:22 CEST 2009
Heinz,
Try this:
lapply(DF, function(x)names(DF)[as.numeric(gsub("[^0-9]", "",
deparse(substitute(x))))])
On Mon, Sep 28, 2009 at 8:43 AM, Heinz Tuechler <tuechler at gmx.at> wrote:
> Thank you, Henrique,
>
> my example was simplified. In a more complexe function I want to use the
> objects, not just their names. In your solution, I have to adapt the
> function itself, depending on the name of the data.frame, which I would like
> to avoid.
>
> Thanks,
> Heinz
>
>
> At 13:36 28.09.2009, Henrique Dallazuanna wrote:
>>
>> You can use names insteed:
>>
>> DF <- data.frame(a=1:3, b=2:4)
>> lapply(names(DF), function(x){
>> print(x)
>> DF[x]
>> })
>>
>> On Mon, Sep 28, 2009 at 8:22 AM, Heinz Tuechler <tuechler at gmx.at> wrote:
>> > 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
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>>
>>
>> --
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>
>
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list