Sort one column based on another column

Hi Tracy, welcome!

You can sort a dataframe by any column or combination of columns with the arrange() function from dplyr, see this example:

library(dplyr)

iris %>%
    arrange(Sepal.Width) %>% 
    head()
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#> 1          5.0         2.0          3.5         1.0 versicolor
#> 2          6.0         2.2          4.0         1.0 versicolor
#> 3          6.2         2.2          4.5         1.5 versicolor
#> 4          6.0         2.2          5.0         1.5  virginica
#> 5          4.5         2.3          1.3         0.3     setosa
#> 6          5.5         2.3          4.0         1.3 versicolor

If you need more specific help, please provide a minimal REPRoducible EXample (reprex) illustrating your issue. A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

2 Likes