[R] attributes of a data.frame

Duncan Murdoch murdoch at stats.uwo.ca
Mon Nov 21 22:00:03 CET 2005

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!

Duncan Murdoch

>> 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
> Thank you for the tip; I'll certainly use it.
> Adrian

More information about the R-help mailing list