[R] how to write assignment form of function
Heinz Tuechler
tuechler at gmx.at
Wed Aug 10 14:24:47 CEST 2005
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
More information about the R-help
mailing list