[R] Changing an unordered factor into an ordered factor

Mathew, Abraham T amathew at ku.edu
Thu Feb 4 16:47:18 CET 2010


1. I need to make it an ordered factor in order to run ordered logit. I could keep it unordered and run a multinomial logit, but that's beyond my capacity as a new social science grad student (can't interpret it)
 
2. this is the sample code I tried before running it on my data.
> x <- c("favor","oppose","havent thought","favor","oppose")
> xf <- factor(x)
> attributes(xf)
$levels
[1] "favor"          "havent thought" "oppose"

$class
[1] "factor"

> levels (xf)
[1] "favor"          "havent thought" "oppose"


> xo <- ordered(x, levels=c("oppose","havent thought","favor"))
> attributes(xo)
$levels
[1] "oppose"         "havent thought" "favor"

$class
[1] "ordered" "factor"

> levels(xo)
[1] "oppose"         "havent thought" "favor"
> cbind(x,xf,xo)
      x                xf  xo
[1,] "favor"          "1" "3"
[2,] "oppose"         "3" "1"
[3,] "havent thought" "2" "2"
[4,] "favor"          "1" "3"
[5,] "oppose"         "3" "1"
> table(x,xf)
                 xf
x                favor havent thought oppose
   favor              2              0      0
   havent thought     0              1      0
   oppose             0              0      2
> table(x,xo)
                 xo
x                oppose havent thought favor
   favor               0              0     2
   havent thought      0              1     0
   oppose              2              0     0



3. This worked fine in my made up scenario, but not when I played with the real data.
 
4.This is a large data set, by my standards at least, but I've included it.
 
 
Thanks for all the help....I'll continue to play with it.
 

________________________________

From: David Winsemius [mailto:dwinsemius at comcast.net]
Sent: Thu 2/4/2010 9:26 AM
To: olihui at gmx.de
Cc: Mathew, Abraham T; r-help at r-project.org
Subject: Re: [R] Changing an unordered factor into an ordered factor




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