[R] stumped on how to reorder factors

Peter Alspach Peter.Alspach at plantandfood.co.nz
Wed Aug 31 05:54:04 CEST 2011


Tena koe Markus

I think you'll have to tailor a solution specific to your case.  However, the following might give you some ideas:

B <- as.factor(c("Engine 1", "Engine 10", "Ladder 3", "Engine 3", "Ladder 10", "Engine 10"))
summary(B)
 Engine 1 Engine 10  Engine 3 Ladder 10  Ladder 3 
        1         2         1         1         1 

ttMat <- as.data.frame(matrix(unlist(strsplit(as.character(B), ' ')), ncol=2, byrow=TRUE))
ttMat <- ttMat[order(ttMat[,1], as.numeric(as.character(ttMat[,2]))),]
B <- factor(B, levels=unique(apply(ttMat, 1, paste, collapse=' ')))
summary(B)
 Engine 1  Engine 3 Engine 10  Ladder 3 Ladder 10 
        1         1         2         1         1 

HTH ....

Peter Alspach

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Markus Weisner
> Sent: Wednesday, 31 August 2011 2:00 p.m.
> To: r-help at r-project.org
> Subject: [R] stumped on how to reorder factors
> 
> I am trying to reorder a factor data type so that when I plot stats
> associated with the factor, the ordering makes sense.
> 
> For instance, if I have a factor entered as follows ...
> 
> A = as.factor(c("1", "10", "3", "3", "10", "10"))
> 
> levels(A)
> 
> 
> ... the ordering does not really make sense (assuming I want the factor
> ordered by integer value), but I understand that this "mis-ordering"
> is
> because the ordering is based on a character string data type and not
> on an
> integer data type.  Because I run into this problem frequently, I wrote
> a
> small function to fix this:
> 
> 
> reorder_factor = function(x, x_sum, decreasing=FALSE){
> 
> factor(as.character(x), levels=levels(x)[order(x_sum,
> decreasing=decreasing)
> ])
> 
> }
> 
> 
> I can then run the following code to fix the problem:
> 
> 
> A = reorder_factor(x=A, x_sum=as.numeric(levels(A)), decreasing=FALSE)
> 
> levels(A)
> 
> 
> ... and now I have correctly ordered integers.  Perhaps not the most
> elegant
> solution, but it worked for my purposes.  Now I have a more complicated
> problem and I need help.  Assuming the following factor:
> 
> 
> B = as.factor(c("Engine 1", "Engine 10", "Ladder 3", "Engine 3",
> "Ladder 10",
> "Engine 10"))
> 
> levels(B)
> 
> 
> I would like the factor ordered first by the proceeding unit type and
> then
> ordered by the following integer.  In this case, I would like to see
> this
> order:  Engine 1, Engine 3, Engine 10, Ladder 3, Ladder 10.  I have
> tried
> many different ways of separating out the unit type from the number,
> but am
> having trouble figuring out a good way of achieving this factor order.
> For
> such a small example, I could obviously manually change the order, but
> I am
> dealing with much larger datasets with many unit types and up to 20
> different numbers for each unit type.  Having an automated way of
> ordering
> these units would be a huge help.  Thanks in advance for any help you
> can
> provide.
> 
> --Markus Weisner
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

The contents of this e-mail are confidential and may be subject to legal privilege.
 If you are not the intended recipient you must not use, disseminate, distribute or
 reproduce all or any part of this e-mail or attachments.  If you have received this
 e-mail in error, please notify the sender and delete all material pertaining to this
 e-mail.  Any opinion or views expressed in this e-mail are those of the individual
 sender and may not represent those of The New Zealand Institute for Plant and
 Food Research Limited.



More information about the R-help mailing list