[R] coercing "numeric" components of data frame to "factor" or "ordered"?
Russell Senior
seniorr at aracnet.com
Fri May 3 01:18:21 CEST 2002
>>>>> "Andy" == apjaworski <apjaworski at mmm.com> writes:
Andy> I am not sure if this is the best way, but one way to do this is
Andy> simply to call the as.factor function. Here is a trivial
Andy> 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)
Andy> Now the x component is a factor.
It looks like if I then do:
> attr(dd$x,"levels") <- c("a","b","d","e")
I can "poke" new factor labels into the data frame (being careful to
align the new alphabetic levels with the previously numeric levels):
> dd
x y
1 a 1.493814499
2 a -1.146225474
3 b 0.004065931
4 b -1.426267694
5 b -0.084461051
6 d -0.094001987
7 e -0.631636358
8 e 0.227095141
I guess that'll work for me. 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list