[R] levels() deletes other attributes

Heinz Tuechler tuechler at gmx.at
Mon Jul 18 16:25:14 CEST 2005


Dear All,

it seems to me that levels() deletes other attributes. See the following
example:

## example with levels
f1 <- factor(c('level c','level b','level a','level c'), ordered=TRUE)
attr(f1, 'testattribute') <- 'teststring'
attributes(f1)
levels(f1) <- c('L-A', 'L-B', 'L-C')
attributes(f1)

If I run it, after assigning new levels, the class is only "factor" instead
of "ordered" "factor" and the $testattribute "teststring" is gone.

The same happens to the label() attribute of Hmisc.

## example with levels and label
library(Hmisc)
f1 <- factor(c('level c','level b','level a','level c'), ordered=TRUE)
label(f1) <- 'factor f1'
attr(f1, 'testattribute') <- 'teststring'
attributes(f1)
levels(f1) <- c('L-A', 'L-B', 'L-C')
attributes(f1)

Should I expect this behaviour?

Thanks

Heinz

# R-Version 2.1.0 Patched (2005-05-30)
# Windows98




More information about the R-help mailing list