Skip to content

Assign factor and levels inside function

1 message · Liaw, Andy

#
Tim,
Ouch!  "levels<-" is generic, and the default method simply attach the
levels attribute to the object.  You need to coerce the object into a factor
explicitly.
I believe the canonical ways of doing something like this in R is something
along the line of:

processData <- function(dat) {
    dat$f1 <- factor(dat$f1, levels=...)
    ...  ## any other manipulations you want to do
    dat
}

Then when you get new data, you just do:

newData <- processData(newData)

HTH,
Andy