[R] levels() deletes other attributes

Heinz Tuechler tuechler at gmx.at
Mon Jul 18 17:10:30 CEST 2005


At 16:53 18.07.2005 +0200, Heinz Tuechler wrote:
>At 09:29 18.07.2005 -0500, Frank E Harrell Jr wrote:
>>Heinz Tuechler wrote:
>>> 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?
>>
>>Does the same thing happen when you do
>>
>>  attr(f1,'levels') <- c('L-A',...)
>>
>>Frank
>
>No, it does not. With attr(f1,'levels') <- c('L-A', 'L-B', 'L-C') only the
>levels are changed, all other attributes remain as before.
>Heinz
>
I think, I know why attr(f1,'levels') behaves different from levels(f1) <- .
As far as I see, the method of levels for factor does not use attr() but
factor().
Heinz
>>> 
>>> Thanks
>>> 
>>> Heinz
>>> 
>>> # R-Version 2.1.0 Patched (2005-05-30)
>>> # Windows98
>>> 
>>> ______________________________________________
>>> 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
>>> 
>>
>>
>>-- 
>>Frank E Harrell Jr   Professor and Chair           School of Medicine
>>                      Department of Biostatistics   Vanderbilt University
>>
>>
>
>______________________________________________
>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
>
>




More information about the R-help mailing list