Arrange a data frame by the possibly the group_vars()
(optional, but defaults to off) and arrangeTerms
. Accepts arbitrary text as
arrangeTerms to allow forms such as "desc(gear)". Intent is to arrange only by
sets of variables with desc() notations reversals, not by arbitrary expressions over variables.
To help enforce this parsing is performed in an empty environment (so expressions
such as "gear + carb" deliberately error-out).
arrange_se(.data, arrangeTerms, ..., .by_group = FALSE, strict = TRUE)
.data | data.frame |
---|---|
arrangeTerms | character vector of column expressions to arrange by. |
... | not used, force later arguments to bind by name. |
.by_group | logical, should data be sorted by grouping variables (if present). |
strict | logical if TRUE accept only name and desc(name) terms. |
.data arrnaged by arrangeTerms
#> mpg cyl disp hp drat wt qsec vs am gear carb #> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 #> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 #> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 #> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 #> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1# equivilent to dplyr/magrittr pipeline # arrange(datasets::mtcars, cyl, desc(gear)) %>% head() # Note: arranging in the presence of groups is subtle. # As grouping is an annotation, not an ordering (and ordering is # unfortunately not an annotation). d <- data.frame(x = 1:6, sin_x = sin(1:6), grp = rep(c("a", "b"), 3), stringsAsFactors = FALSE) # arranged by sin_x and not by grp d %.>% group_by_se(., "grp") %.>% arrange_se(., "sin_x")#> # A tibble: 6 x 3 #> # Groups: grp [2] #> x sin_x grp #> <int> <dbl> <chr> #> 1 5 -0.959 a #> 2 4 -0.757 b #> 3 6 -0.279 b #> 4 3 0.141 a #> 5 1 0.841 a #> 6 2 0.909 b#> # A tibble: 6 x 3 #> # Groups: grp [2] #> x sin_x grp #> <int> <dbl> <chr> #> 1 5 -0.959 a #> 2 4 -0.757 b #> 3 6 -0.279 b #> 4 3 0.141 a #> 5 1 0.841 a #> 6 2 0.909 b# arranged by sin_x and not by grp d %.>% group_by_se(., "grp") %.>% arrange_se(., "sin_x", .by_group = TRUE)#> # A tibble: 6 x 3 #> # Groups: grp [2] #> x sin_x grp #> <int> <dbl> <chr> #> 1 5 -0.959 a #> 2 3 0.141 a #> 3 1 0.841 a #> 4 4 -0.757 b #> 5 6 -0.279 b #> 6 2 0.909 b# arranged by sin_x and not by grp d %.>% arrange_se(., "sin_x", .by_group = TRUE) %.>% group_by_se(., "grp")#> # A tibble: 6 x 3 #> # Groups: grp [2] #> x sin_x grp #> <int> <dbl> <chr> #> 1 5 -0.959 a #> 2 4 -0.757 b #> 3 6 -0.279 b #> 4 3 0.141 a #> 5 1 0.841 a #> 6 2 0.909 b