Order rows of database tables by an expression involving its variables.

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

Arguments

.data

A tbl. All main verbs are S3 generics and provide methods for tbl_df(), dtplyr::tbl_dt() and dbplyr::tbl_dbi().

...

Comma separated list of unquoted variable names, or expressions involving variable names. 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

An object of the same class as .data.

Missing values

Compared to its sorting behaviour on local data, the arrange() method for most database tables sorts NA at the beginning unless wrapped with desc(). Users can override this behaviour by explicitly sorting on is.na(x).

Examples

library(dplyr)
#> #> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:dbplyr’: #> #> ident, sql
#> The following objects are masked from ‘package:stats’: #> #> filter, lag
#> The following objects are masked from ‘package:base’: #> #> intersect, setdiff, setequal, union
dbplyr::memdb_frame(a = c(3, 4, 1, 2)) %>% arrange(a)
#> # Source: table<dbplyr_001> [?? x 1] #> # Database: sqlite 3.22.0 [:memory:] #> # Ordered by: a #> a #> <dbl> #> 1 1 #> 2 2 #> 3 3 #> 4 4
# NA sorted first dbplyr::memdb_frame(a = c(3, 4, NA, 2)) %>% arrange(a)
#> # Source: table<dbplyr_002> [?? x 1] #> # Database: sqlite 3.22.0 [:memory:] #> # Ordered by: a #> a #> <dbl> #> 1 NA #> 2 2 #> 3 3 #> 4 4
# override by sorting on is.na() first dbplyr::memdb_frame(a = c(3, 4, NA, 2)) %>% arrange(is.na(a), a)
#> # Source: table<dbplyr_003> [?? x 1] #> # Database: sqlite 3.22.0 [:memory:] #> # Ordered by: is.na(a), a #> a #> <dbl> #> 1 2 #> 2 3 #> 3 4 #> 4 NA