Reorder factors numerically in a data frame
r, sorting
Solution
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)
Problem
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.