Sorting by Single Column

We wish to sort the rows of a data frame by the values of one column.

Ascending

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:

  • We pass the data frame df to the function arrange().
  • We pass to arrange() the name of the column we wish to sort by which here is col_1.
  • arrange() sorts in ascending (increasing) order by default.

Descending

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:

  • We pass the data frame df to the function arrange().
  • We pass to arrange() the name of the column we wish to sort by which here is col_1.
  • By default arrange() sorts in ascending order. To sort in descending order, we wrap the column name in the function desc(); here arrange(desc(col_1)).
R
I/O