Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorder factors numerically in a data frame

Tags:

sorting

r

I have factors from 0 to 39. Here is how they are ordered now:

> levels(items$label)
 [1] "0"  "1"  "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
[13] "2"  "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "3" 
[25] "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "4"  "5" 
[37] "6"  "7"  "8"  "9"

How do I reorder them in numeric order, for display purposes? I don't want to change the meaning of the data frame.

UPDATE: How do I update the original data frame, items, with the sorted factor, labels? This should not change the data frame substantively; I just want the factors to come out in the correct order in subsequent operations.

like image 842
David J. Avatar asked Mar 27 '13 17:03

David J.


People also ask

How do you reorder a factor level?

One way to change the level order is to use factor() on the factor and specify the order directly. In this example, the function ordered() could be used instead of factor() . Another way to change the order is to use relevel() to make a particular level first in the list.

How do I sort numerically in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.

How do you reorder the levels of a variable in R?

Occasionally you may want to re-order the levels of some factor variable in R. Fortunately this is easy to do using the following syntax: factor_variable <- factor(factor_variable, levels=c('this', 'that', 'those', ...))

How do I order a factor column in R?

To sort a numerical factor column in an R data frame, we would need to column with as. character then as. numeric function and then order function will be used.


1 Answers

sorted_labels <- paste(sort(as.integer(levels(items$label))))

Gives:

 [1] "0"  "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11"
[13] "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23"
[25] "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35"
[37] "36" "37" "38" "39"

Or (as mentioned in https://stackoverflow.com/a/15665655/109618):

sorted_labels <- order(levels(items$label)) - 1
# order by itself is a 1-based vector
# using `- 1` gives a 0-based vector

Per the updated question, this updates the data frame:

items$label <- factor(items$label, levels = sorted_labels)
like image 182
David J. Avatar answered Sep 28 '22 20:09

David J.