Dear all, I noticed that a data.frame has four attributes: - names - row.names - class - variable.labels While one can use the first three (i.e. names(foo) or class(foo)), the fourth one can only be used via: attributes(foo)$variable.labels (which is kind of a tedious thing to type) Is it or would be possible to simply use: variable.labels(foo) like the first three attributes? I tried: varlab <- function(x) attributes(x)$variable.labels but then I cannot use this to assign a specific label:> varlab(foo)[1] <- "some string"Error: couldn't find function "varlab<-" Thank you, Adrian -- Adrian DUSA Romanian Social Data Archive 1, Schitu Magureanu Bd 050025 Bucharest sector 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101
?assign -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Adrian DUSA > Sent: Monday, November 21, 2005 11:19 AM > To: r-help at stat.math.ethz.ch > Subject: [R] attributes of a data.frame > > > Dear all, > > I noticed that a data.frame has four attributes: > - names > - row.names > - class > - variable.labels > > While one can use the first three (i.e. names(foo) or > class(foo)), the fourth > one can only be used via: > attributes(foo)$variable.labels > (which is kind of a tedious thing to type) > > Is it or would be possible to simply use: > variable.labels(foo) > like the first three attributes? > > I tried: > varlab <- function(x) attributes(x)$variable.labels > > but then I cannot use this to assign a specific label: > > varlab(foo)[1] <- "some string" > Error: couldn't find function "varlab<-" > > Thank you, > Adrian > > -- > Adrian DUSA > Romanian Social Data Archive > 1, Schitu Magureanu Bd > 050025 Bucharest sector 5 > Romania > Tel./Fax: +40 21 3126618 \ > +40 21 3120210 / int.101 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
No! Ignore my previous "advice" . To assign attrributes of anything, see ?attributes. Shame on me! -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Adrian DUSA > Sent: Monday, November 21, 2005 11:19 AM > To: r-help at stat.math.ethz.ch > Subject: [R] attributes of a data.frame > > > Dear all, > > I noticed that a data.frame has four attributes: > - names > - row.names > - class > - variable.labels > > While one can use the first three (i.e. names(foo) or > class(foo)), the fourth > one can only be used via: > attributes(foo)$variable.labels > (which is kind of a tedious thing to type) > > Is it or would be possible to simply use: > variable.labels(foo) > like the first three attributes? > > I tried: > varlab <- function(x) attributes(x)$variable.labels > > but then I cannot use this to assign a specific label: > > varlab(foo)[1] <- "some string" > Error: couldn't find function "varlab<-" > > Thank you, > Adrian > > -- > Adrian DUSA > Romanian Social Data Archive > 1, Schitu Magureanu Bd > 050025 Bucharest sector 5 > Romania > Tel./Fax: +40 21 3126618 \ > +40 21 3120210 / int.101 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
On 11/21/2005 2:18 PM, Adrian DUSA wrote:> Dear all, > > I noticed that a data.frame has four attributes: > - names > - row.names > - class > - variable.labels > > While one can use the first three (i.e. names(foo) or class(foo)), the fourth > one can only be used via: > attributes(foo)$variable.labels > (which is kind of a tedious thing to type) > > Is it or would be possible to simply use: > variable.labels(foo) > like the first three attributes? > > I tried: > varlab <- function(x) attributes(x)$variable.labels > > but then I cannot use this to assign a specific label: >> varlab(foo)[1] <- "some string" > Error: couldn't find function "varlab<-"Not all dataframes have the variable.labels attribute. I'm guessing you've installed some contributed package to add them, or are importing an SPSS datafile using read.spss. So don't expect varlab() or variable.labels() function to be a standard R function. If you want to define it, definitions like this should work (but I can't test them): varlab <- function(foo) attr(foo, "variable.labels") "varlab<-" <- function(foo, label, value) { attr(foo, "variable.labels")[label] <- value foo } Use them like this: varlab(x) # to see the labels varlab(x, "varname") <- "label" # to set one Duncan Murdoch
It's hard for me to resist dipping my oar into this... Variable labels are so generally useful, both in documenting a dataset (what was 'pctx723' again?) and in producing readable output and graphs that it is a shame they are not provided in base R. If they were (and were used in print and plot methods, when available) it would avoid a lot of the necessity to specify xlab= and ylab= in graphs, or, perhaps worse, ending up with pctx723 as the label in your presentation. -Michael> On 11/21/2005 2:51 PM, Adrian DUSA wrote: > >>> On Monday 21 November 2005 22:41, Duncan Murdoch wrote: >> >>>>> [...snip...] >>>>> Not all dataframes have the variable.labels attribute. I'm guessing >>>>> you've installed some contributed package to add them, or are importing >>>>> an SPSS datafile using read.spss. So don't expect varlab() or >>>>> variable.labels() function to be a standard R function. >> >>> >>> Aa-haa... of course you are right: I read them via read.spss. I understand. >>> Now, just to the sake of it, would it be wrong to make it standard? >>> Is there a special reason not to? > > > I think it's just that the R core developers don't see the need for > them. If something is worth documenting, then you should write an .Rd > file or a vignette about it, and that gives you more flexibility than a > one line label. > > I think there are definitely developers out there who disagree with this > point of view, and I'm pretty sure I've seen a contributed package that > offered support for this, but I can't remember which one right now. So > that's another reason why it's not in the base: it doesn't need to be, > you can just go find and install that contributed package! >-- Michael Friendly Email: friendly AT yorku DOT ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html Toronto, ONT M3J 1P3 CANADA