[R] attributes of a data.frame
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Nov 21 21:41:06 CET 2005
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
More information about the R-help
mailing list