On Tue, 8 Oct 2002, [iso-8859-1] Göran Broström wrote:
> I find 'How do I convert factors to numeric?' in the FAQ (7.12),
but not
> the other way around. Trivial maybe, but
>
> > codes(factor(c(2, 10))
> [1] 2 1
> > codes(factor(c(2, 9)))
> [1] 1 2
>
> How do I get the levels attached to the codes in numeric order?
>
The short answer is that you don't. If you care about the order of the
codes then you have an ordered factor> codes(ordered(c(2, 10)))
[1] 1 2> codes(ordered(c(2, 9)))
[1] 1 2
As the code for codes.factor is
function (x, ...)
{
rank(levels(x))[x]
}
you can't change the order from the default, lexicographic order.
Perhaps your problem is that you think codes() means something (eg the
numbers used internally to code the factor).
This may be illuminating:> unclass(factor(c(2,9)))
[1] 1 2
attr(,"levels")
[1] "2" "9"> unclass(factor(c(2,10)))
[1] 1 2
attr(,"levels")
[1] "2" "10"> as.integer(factor(c(2,9)))
[1] 1 2> as.integer(factor(c(2,10)))
[1] 1 2>
-thomas
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._