# Re: [R] how to write assignment form of function

From: Heinz Tuechler <tuechler_at_gmx.at>
Date: Thu 11 Aug 2005 - 07:25:16 EST

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@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@stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
http://www.R-project.org/posting-guide.html
>>
>>
>>
>>
>>
>
>

R-help@stat.math.ethz.ch mailing list