Russell Senior
2002-May-02 21:45 UTC
[R] coercing "numeric" components of data frame to "factor" or "ordered"?
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
apjaworski@mmm.com
2002-May-02 22:34 UTC
[R] coercing "numeric" components of data frame to "factor" or "ordered"?
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
John Fox
2002-May-03 01:03 UTC
[R] coercing "numeric" components of data frame to "factor" or "ordered"?
Dear Russell, At 02:45 PM 5/2/2002 -0700, you wrote:>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!Actually, you can do exactly what you suggest. Say you have a data frame df with numeric variables one and two: attr(df$one, 'levels') <- sort(unique(df$one)) class(df$one) <- 'factor' attr(df$two, 'levels') <- sort(unique(df$two)) class(df$two) <- c('ordered', 'factor') But it's probably simpler just to do df$one <- factor(df$one) df$two <- ordered(df$two) I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._