Search code examples
rdataframerow

Reorder rows in R dataframe by column value


I have a dataframe called tab12 that look like this:

df <- structure(list(sector = c("Public", "Private", "Other"), mean = c(340L, 
125L, 654L), median = c(876L, 543L, 234L)), class = "data.frame", row.names = c(NA, 
-3L))

sector  mean    median
Public  340     876
Private 125     543
Other   654     234

I need to reorder the rows so that the order is "Other", "Public", "Private". I know I can do this by index (e.g, [c(3, 1, 2), ] but is there a way to do this by using the values of the sector column ("Public", "Private" and "Other")?

I have tried:

tab12 <- tab12$Sector[c("Other", "Public", "Private")]

Any help or direction would be much appreciated. Thanks


Solution

  • character vectors (like your sector column) will by default be sorted alphabetically if you order them. You can convert them to a factor-class categorical vector which you can define a custom order, and then sort.

    If you like dplyr:

    tab12 %>%
      mutate(Sector = factor(Sector, levels = c("Other", "Public", "Private")) %>%
      arrange(Sector)
    

    If you prefer base R:

    tab12$Sector = factor(tab12$Sector, levels = c("Other", "Public", "Private"))
    tab12 = tab12[order(tab12$Sector), ]
    

    I prefer encoding a custom ordering like this into a factor rather than just re-ordering the rows for a few reasons:

    • It generalizes nicely to other functions (e.g., plot axes, legends, modeling)
    • It works whether you have one or multiple of each value