[R] coercing "numeric" components of data frame to "factor" or "ordered"?

apjaworski@mmm.com apjaworski at mmm.com
Fri May 3 00:34:29 CEST 2002


I am not sure if this is the best way, but one way to do this is simply to
call the as.factor function.  Here is a trivial example.

> x <- c(1, 1, 2, 2, 2, 4, 5, 5)
> y <- rnorm(length(x))
> dd <- data.frame(x, y)
> dd$x <- as.factor(dd$x)

Now the x component is a factor.

Cheers,

Andy

__________________________________
Andy Jaworski
Engineering Systems Technology Center
3M Center, 518-1-01
St. Paul, MN 55144-1000
-----
E-mail: apjaworski at mmm.com
Tel:  (651) 733-6092
Fax:  (651) 736-3122


                                                                                                                                               
                    Russell Senior                                                                                                             
                    <seniorr at aracne      To:     r-help at stat.math.ethz.ch                                                                      
                    t.com>               cc:     (bcc: Andrzej P. Jaworski/US-Corporate/3M/US)                                                 
                                         Subject:     [R] coercing "numeric" components of data frame to "factor" or "ordered"?                
                    05/02/2002                                                                                                                 
                    16:45                                                                                                                      
                                                                                                                                               
                                                                                                                                               






I am getting ready to load a bunch of data into R.  The data is all
numeric, but some of the numbers are integer codes representing
non-numeric semantics.  What is the best way to "fix" the data frames
so that these compenents are recognized as "factors" or "ordered", as
appropriate?

Can I "assign" to some attribute of the data frame component, like the
"class" or "levels" to get the desired effect?

Thanks!

--
Russell Senior         ``The two chiefs turned to each other.
seniorr at aracnet.com      Bellison uncorked a flood of horrible
                         profanity, which, translated meant, `This is
                         extremely unusual.' ''
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._._




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