Re: [R] How to retrieve a vector of a data.frame's variable attributes?

From: Bacou, Melanie <mel_at_mbacou.com>
Date: Sun, 17 Apr 2011 23:00:12 -0400

David,

Thanks very much. That was the right method.

--Mel.

-----Original Message-----
From: David Winsemius [mailto:dwinsemius_at_comcast.net] Sent: Sunday, April 17, 2011 6:00 PM
To: Bacou, Melanie
Cc: r-help_at_r-project.org
Subject: Re: [R] How to retrieve a vector of a data.frame's variable attributes?

On Apr 17, 2011, at 4:21 PM, Bacou, Melanie wrote:

> Hi,
>
> I have a data.frame with 100 variables and I have assigned a "label",
> "units" and "category" attribute to each variable. I would like to
> reorder
> the variables in the data.frame by the "category" attributes but
> can't find
> a way.
>

Something like lapply(hh, attr, which="category") might return something potentially useful. You obviously have a test case, but have failed to offer it up. Possibly using order() around that might get all the like category variables together.

> For example, the first variable is:
>
>> attributes(hh$aez)
> $levels
> [1] "coastal" "forest" "savannah"
>
> $class
> [1] "labelled" "factor"
>
> $label
> [1] ecological zone
> 93 Levels: 10 quantiles of welfare ... year of the interview
>
> $units
> [1] class
> 24 Levels: '05 PPP USD / year cedis / year cedis /year class
> geo-1 ... years
>
> $category
> [1] geography
> 7 Levels: agriculture demography design expenditure geography ...
> welfare
>
> I have tried:
>
> hh <- hh[, order(attr(hh, "category")) ]

Did you look at what order(attr(hh, "category")) returns. Since you assigned the attribute to individual columns (which are arranged as a list, you cannot expect the whole object to return anything useable when queried with attr().

> hh <- hh[, order(attr(hh[, 1:100], "category"))]

(It would be the same since hh == hh[,1:100] )

> hh <- hh[, order(attr(dimnames(hh), "category"))]

dimnames would _not_ have any attributes. And attr can only work on one object at a a time anyway,

>
> but all the right-hand side assignments above return NULL.
>
> Thanks very much for your help with this simple task!
>
> --Mel.
>
>
>
> _______________________
> Melanie Bacou

David Winsemius, MD
West Hartford, CT



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. Received on Mon 18 Apr 2011 - 05:09:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Apr 2011 - 06:20:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive