[R] Changing an unordered factor into an ordered factor
David Winsemius
dwinsemius at comcast.net
Thu Feb 4 16:26:41 CET 2010
On Feb 4, 2010, at 5:58 AM, Oliver Gondring wrote:
> Hi Mathew,
>
>> I'm trying to change an unordered factor into an ordered factor:
>> Help
>
>
> it's all about reordering the factor levels, right?
It's hard to tell. You could be correct, but the OP never described
his reasons for wanting "ordered" factors. The page you offer below
only describes the "ordering" or "releveling" of the levels in
_unordered_ factors and does not describe creating or working with
ordered factors which are of a different class.
Ordered factor have additional methods
> x <- factor(sample(c("Oppose", "Haven't thought much about this",
"Favor"), 10, replace=TRUE) )
> x
[1] Oppose Favor
Oppose
[4] Favor Haven't thought much about this
Oppose
[7] Oppose Oppose
Oppose
[10] Oppose
Levels: Favor Haven't thought much about this Oppose
> ox <- factor(x, levels=c("Haven't thought much about
this","Oppose", "Favor"), ordered=TRUE)
> ox
[1] Oppose Favor
Oppose
[4] Favor Haven't thought much about this
Oppose
[7] Oppose Oppose
Oppose
[10] Oppose
Levels: Haven't thought much about this < Oppose < Favor # Note the
difference here.
> ox[1] < ox[3]
[1] FALSE # the < operator is meaningful here
> x[1] < x[3]
[1] NA
Warning message:
In Ops.factor(x[1], x[3]) : < not meaningful for factors
>
>
> Have a look at this one:
> http://rwiki.sciviews.org/doku.php?id=tips:data-factors:factors
>
> The third textbox on the page (search for the string: "Reorder factor
> levels") provides a piece of sample code for the task. Should be
> easy to
> adapt it to your problem.
>
> Oliver
>
> P.S.: Maybe it's not a bad idea to provide a more readable
> transformation of your code next time. What about replacing the whole
> 'V961327' thing by 'x' for example?
>
And maybe some sample data as well?
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list