[R] Assign factor and levels inside function

From: Tim Howard <tghoward_at_gw.dec.state.ny.us>
Date: Thu 21 Apr 2005 - 05:05:35 EST


R-help,
  After cogitating for a while, I finally figured out how to define a data.frame column as factor and assign the levels within a function... BUT I still need to pass the data.frame and its name separately. I can't seem to find any other way to pass the name of the data.frame, rather than the data.frame itself. Any suggestions on how to go about it? Is there something like value(object) or name(object) that I can't find?

#sample dataframe for this example
y <- data.frame(
 one=c(1,1,3,3,5,7),
 two=c(2,2,6,6,8,8))

> levels(y$one) # check out levels

NULL # the function I've come up with
fncFact <- function(datfra, datfraNm){
datfra$one <- factor(datfra$one, levels=c(1,3,5,7,9)) assign(datfraNm, datfra, pos=1)
}

>fncFact(y, "y")
> levels(y$one)

[1] "1" "3" "5" "7" "9"

I suppose only for aesthetics and simplicity, I'd like to have only pass the data.frame and get the same result. Thanks in advance,
Tim Howard

> version

         _
platform i386-pc-mingw32

arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    2              
minor    0.1            
year     2004           
month    11             
day      15             

language R

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 Thu Apr 21 06:03:25 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:18 EST