We wish to sort the rows of a data frame by the values of one column.
We wish to sort a data frame in increasing order of the values of one column.
In this example, we wish to sort a data frame df
in ascending order of the values of a column col_1
.
df = df %>% arrange(col_1)
Here is how this works:
df
to the function arrange()
.arrange()
the name of the column we wish to sort by which here is col_1
.arrange()
sorts in ascending (increasing) order by default.We wish to sort a data frame in descending order of the values of one column.
In this example, we wish to sort a data frame df
in descending order of the values of a column col_1
.
df = df %>% arrange(desc(col_1))
Here is how this works:
df
to the function arrange()
.arrange()
the name of the column we wish to sort by which here is col_1
.arrange()
sorts in ascending order. To sort in descending order, we wrap the column name in the function desc()
; here arrange(desc(col_1))
.