[R] how to write assignment form of function

Heinz Tuechler tuechler at gmx.at
Wed Aug 10 23:25:16 CEST 2005

At 17:44 10.08.2005 +0100, Patrick Burns wrote:
>S Poetry may be of use to you in this.

Thank you for the hint and thank you for S Poetry. I like it, I read it,
maybe about a year ago, but I don't remember all of it and with all the
material I collected about R it's not always easy to remember where I read

Heinz Tüchler
>Patrick Burns
>patrick at burns-stat.com
>+44 (0)20 8525 0696
>(home of S Poetry and "A Guide for the Unwilling S User")
>Heinz Tuechler wrote:
>>Dear All,
>>where can I find information about how to write an assigment form of a
>>For curiosity I tried to write a different form of the levels()-function,
>>since the original method for factor deletes all other attributes of a
>>Of course, the simple method would be to use instead of levels(x) <-
>>newlevels, attr(x, 'levels') <- newlevels.
>>I tried the following:
>>## example
>>x <- factor(c(1,1,NA,2,3,4,4,4,1,2)); y <- x
>>attr(x, 'levels') <- c('a', 'b', 'c', 'd')     # does what I want
>> [1] a    a    <NA> b    c    d    d    d    a    b   
>>Levels: a b c d
>>'levels.simple<-' <- function (x, value) 
>> {
>>      attr(x, 'levels') <- value
>> }
>>levels.simple(y) <- c('a', 'b', 'c', 'd')     # does not what I want
>>[1] "a" "b" "c" "d"
>>Heinz Tüchler
>>R-help at stat.math.ethz.ch mailing list
>>PLEASE do read the posting guide!

More information about the R-help mailing list