[R] how to write assignment form of function

Heinz Tuechler
Wed, 10 Aug 2005 21:25:16 -0000

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
what.

Heinz T??chler
>
>
>Patrick Burns
>patrick at burns-stat.com
>+44 (0)20 8525 0696
>http://www.burns-stat.com
>(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
>>function?
>>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
factor.
>>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
>>x
>> [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
>>y
>>[1] "a" "b" "c" "d"
>>
>>Thanks,
>>Heinz T??chler
>>
>>______________________________________________
>>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
>>
>>
>>
>>
>>
>
>