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

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Wed 10 Aug 2005 - 22:49:08 EST

take a look at e.g., get("levels<-.factor"); you should return "x" in your function, i.e.,

y <- x <- factor(c(1,1,NA,2,3,4,4,4,1,2)) ############
'levels.simple<-' <- function(x, value){

      attr(x, 'levels') <- value
      x

 }

levels.simple(y) <- c('a', 'b', 'c', 'd') y

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


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 PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

R-help@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 Received on Wed Aug 10 22:53:20 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:45 EST