Skip to contents

arrange_natural() orders the rows of a data frame by the values of selected columns.

Data is naturally sorted (see Details, below) in ascending order. Columns used for sorting are converted to factors to preserve their ordering. Grouping is ignored.

Usage

arrange_natural(.data, ...)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

...

<data-masking> Variables to sort by.

Value

An object of the same type as .data. The output has the following properties:

  • All rows appear in the output, but (usually) in a different place.

  • Sorted columns are converted to factors.

  • All other columns are not modified.

  • Data frame attributes are preserved.

Details

Natural sorting

arrange_natural() is built on dplyr::arrange() to provide natural sorting (sorting of strings with both letters and numerals). The underlying implementation for natural sorting is based on the stringi library.

See also

Examples

df <- tibble::tribble(
  ~sample, ~gene,
  "D10-23", "atp6",
  "D10-43", "mdr1",
  "D10-55", "atp6",
  "D10-5", "mdr1",
  "D10-47", "dhps",
  "D10-15", "atp6"
)

arrange_natural(df, sample)
#> # A tibble: 6 × 2
#>   sample gene 
#>   <fct>  <chr>
#> 1 D10-5  mdr1 
#> 2 D10-15 atp6 
#> 3 D10-23 atp6 
#> 4 D10-43 mdr1 
#> 5 D10-47 dhps 
#> 6 D10-55 atp6 
df %>% arrange_natural(sample, gene)
#> # A tibble: 6 × 2
#>   sample gene 
#>   <fct>  <fct>
#> 1 D10-5  mdr1 
#> 2 D10-15 atp6 
#> 3 D10-23 atp6 
#> 4 D10-43 mdr1 
#> 5 D10-47 dhps 
#> 6 D10-55 atp6