Heinz Tuechler
2009-Sep-28 11:22 UTC
[R] How to assess object names within a function in lapply or l_ply?
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
Henrique Dallazuanna
2009-Sep-28 11:36 UTC
[R] How to assess object names within a function in lapply or l_ply?
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
2009-Sep-28 11:57 UTC
[R] How to assess object names within a function in lapply or l_ply?
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
hadley wickham
2009-Sep-28 14:27 UTC
[R] How to assess object names within a function in lapply or l_ply?
> 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/
hadley wickham
2009-Sep-28 16:17 UTC
[R] How to assess object names within a function in lapply or l_ply?
> 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.lapply knows the names - the calling function doesn't - it takes the output add then fixes up the names after it's run. Hadley -- http://had.co.nz/