[R] How to coerce data frame elements into factors?

Peter Dalgaard BSA p.dalgaard at pubhealth.ku.dk
Thu Mar 1 21:08:02 CET 2001


Andrew Perrin <aperrin at socrates.berkeley.edu> writes:

> Greetings -
> 
> In trying to do a multiple correspondence analysis, I need to coerce a
> data frame such that each of its columns will be a factor.  Can someone
> offer advice on how to make this happen?  I've tried this:
> 
> > logic.resources.f.df<-apply(logics.resources.df, 2, factor)
> 
> but:
> 
> > is.factor(logic.resources.f.df$interests)
> [1] FALSE

apply will give you a vector or array, so you're doing is.factor(NULL) 

> 
> 
> I've also tried individually,
> 
> > foo<-data.frame(c(factor(mediate), factor(morality)))

> > is.factor(foo$mediate)
> [1] FALSE

What gives you the idea that that would have a component called
"mediate"? 

> x<-1:0
> y<-0:1
> data.frame(c(factor(x),factor(y)))
  c.factor.x...factor.y..
1                       2
2                       1
3                       1
4                       2

Try this:

> d<-data.frame(a=0:1,b=1:0)
> d<-as.data.frame(lapply(d,factor)) 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list