[R] Changing an unordered factor into an ordered factor

David Winsemius dwinsemius at comcast.net
Thu Feb 4 17:24:59 CET 2010


I'm guessing that the attachment did not make it to the list, since it  
was of a filetype that is not on the approved list and also because it  
was 5.4MB long. My mail-server has no such restrictions, so I do have  
your file of type ".POR". Unfortunately I have serious reservations  
about opening files of uncertain heritage from persons about whom I  
know very little.

Your reasons for wanting an ordered factor sound sensible to me. My  
suggestion would be to extract a more modestly sized portion of the  
data ... perhaps 40KB ...  and submit it as an attachment of type .txt.

-- 
David


On Feb 4, 2010, at 10:47 AM, Mathew, Abraham T wrote:

> 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"

That looked successful. I think the appropriate way to "look" at that  
data would be summary() or one of the describe() functions. ( I use  
Hmisc::describe(). )


>
>> levels(xo)
> [1] "oppose"         "havent thought" "favor"
>> cbind(x,xf,xo)

I am not sure what it means to cbind factors. Is that a maneuver that  
you have done successfully before? I guess it "worked".


>      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)

And why would you do such a thing?  You are now cross tabulating the  
internal factor numbers. You know what the answer should be.



>                 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.

"Played"?  What was the code?

>
> 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
>
>
>
> <anes1996.POR>

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list