Skip to content

This is an method for the dplyr arrange() generic. It generates the ORDER BY clause of the SQL query. It also affects the window_order() of windowed expressions in mutate.tbl_lazy().

Note that ORDER BY clauses can not generally appear in subqueries, which means that you should arrange() as late as possible in your pipelines.

Usage

# S3 method for tbl_lazy
arrange(.data, ..., .by_group = FALSE)

Arguments

.data

A lazy data frame backed by a database query.

...

<data-masking> Variables, or functions of variables. Use desc() to sort a variable in descending order.

.by_group

If TRUE, will sort first by grouping variable. Applies to grouped data frames only.

Value

Another tbl_lazy. Use show_query() to see the generated query, and use collect() to execute the query and return data to R.

Missing values

Unlike R, most databases sorts NA (NULLs) at the front. You can can override this behaviour by explicitly sorting on is.na(x).

Examples

library(dplyr, warn.conflicts = FALSE)

db <- memdb_frame(a = c(3, 4, 1, 2), b = c(5, 1, 2, NA))
db %>% arrange(a) %>% show_query()
#> <SQL>
#> SELECT `dbplyr_SwlKLUUEdL`.*
#> FROM `dbplyr_SwlKLUUEdL`
#> ORDER BY `a`

# Note that NAs are sorted first
db %>% arrange(b)
#> # Source:     SQL [4 x 2]
#> # Database:   sqlite 3.45.2 [:memory:]
#> # Ordered by: b
#>       a     b
#>   <dbl> <dbl>
#> 1     2    NA
#> 2     4     1
#> 3     1     2
#> 4     3     5
# override by sorting on is.na() first
db %>% arrange(is.na(b), b)
#> # Source:     SQL [4 x 2]
#> # Database:   sqlite 3.45.2 [:memory:]
#> # Ordered by: is.na(b), b
#>       a     b
#>   <dbl> <dbl>
#> 1     4     1
#> 2     1     2
#> 3     3     5
#> 4     2    NA