The dplyr
function arrange()
allows for reordering data frames and tables based on the content of one or more variables. The function is quite simple and sorts all variables in ascending order by default.
Here is an example where the variable age
is sorted:
Orange %>% arrange(age)
To sort the data frame in descending order, we shall add the helper function desc()
:
Orange %>% arrange(desc(age))
It is possible to sort a data frame according to more than one variable, in which case the sorting process follows the order of the variables between the parentheses of arrange()
. In addition, it is possible to decide for each variable whether the order will be ascending or descending:
Orange %>% arrange(desc(age), circumference)
Fant du det du lette etter? Did you find this helpful?
[Average: 0]