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

Andrew Perrin aperrin at socrates.berkeley.edu
Thu Mar 1 21:27:23 CET 2001


Thank you - that did it. See comments below.

On 1 Mar 2001, Peter Dalgaard BSA wrote:

> Andrew Perrin <aperrin at socrates.berkeley.edu> writes:
> 
[snip]
> > > is.factor(logic.resources.f.df$interests)
> > [1] FALSE
> 
> apply will give you a vector or array, so you're doing is.factor(NULL) 
> 

Got it - thanks for that.

> > 
> > 
> > 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"? 
> 

Well, the fact that my original data frame contained a component called
"mediate". And that when I did the apply() version above, the resulting
data frame did contain "mediate" as determined using colnames().

> > 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)) 
> 

Worked! Thanks - here's what I did:

> logic.resources.f.df<-as.data.frame(lapply(logics.resources.df,factor))
> colnames(logic.resources.f.df)
 [1] "mediate"     "interests"   "enlightened" "morality"    "capacity"   
 [6] "comparison"  "information" "doubt"       "mistrust"    "actors"     
[11] "metalogic"   "policy"     
> is.factor(logic.resources.f.df$mediate)
[1] TRUE


> -- 
>    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
> 

----------------------------------------------------------------------
Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology
Chapel Hill, North Carolina, USA - http://demog.berkeley.edu/~aperrin
        aperrin at socrates.berkeley.edu - aperrin at igc.apc.org


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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