[R] Assign factor and levels inside function

Liaw, Andy andy_liaw at merck.com
Wed Apr 20 22:03:24 CEST 2005


Wouldn't it be easier to do this?

> levels(y$one) <- seq(1, 9, by=2)
> y$one
[1] 1 1 3 3 5 7
attr(,"levels")
[1] 1 3 5 7 9

Andy

> From: Tim Howard
> 
> 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 at 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
> 
> 
>




More information about the R-help mailing list