Vectorised approach to group operations.

bys_count(by)

bys_rank(..., by = NULL, from_last = FALSE)

bys_position(val, by = NULL, from_last = FALSE, ordered = TRUE)

bys_val(..., val, by = NULL, from_last = FALSE)

bys_nval(..., val, by = NULL, from_last = FALSE, n = 1, nmax = FALSE)

bys_min(val, by = NULL, na.rm = TRUE)

bys_max(val, by = NULL, na.rm = TRUE)

bys_sum(val, by = NULL, na.rm = TRUE)

bys_prod(val, by = NULL, na.rm = TRUE)

bys_cummin(val, by = NULL, na.rm = TRUE)

bys_cummax(val, by = NULL, na.rm = FALSE)

bys_cumsum(val, by = NULL, na.rm = TRUE)

bys_cumprod(val, by = NULL, na.rm = TRUE)

bys_lag(val, by = NULL, n = 1)

bys_lead(val, by = NULL, n = 1)

Arguments

by

[atomic]. Groups.

...

[atomic]. Sort levels

from_last

[logical] Sort order - TRUE (descending) or FALSE (ascending).

val

[atomic]. Value

ordered

If TRUE, values are sequential.

n

[integer] Position.

nmax

[logical] If TRUE, use length([by]) when n is greater than the number of records in a group.

na.rm

If TRUE, remove NA values

Value

[atomic]

Examples

x <- data.frame(
  group = c(2, 2, 1, 2, 1, 1, 1, 2, 1, 1),
  value = c(13, 14, 20, 9, 2, 1, 8, 18, 3, 17))

bys_count(x$group)
#>  [1] 4 4 6 4 6 6 6 4 6 6
bys_position(x$value, by = x$group, from_last = TRUE)
#>  [1] 3.1 2.1 1.1 4.1 5.1 6.1 3.1 1.1 4.1 2.1
bys_rank(by = x$group, val = x$value, from_last = TRUE)
#>  [1] 3 2 1 4 5 6 3 1 4 2
bys_val(x$value, by = x$group, val = x$value, from_last = TRUE)
#>  [1] 18 18 20 18 20 20 20 18 20 20
bys_nval(x$value, by = x$group, val = x$value, from_last = TRUE, n = 2)
#>  [1] 14 14 17 14 17 17 17 14 17 17
bys_min(by = x$group, val = x$value)
#>  [1] 9 9 1 9 1 1 1 9 1 1
bys_max(by = x$group, val = x$value)
#>  [1] 18 18 20 18 20 20 20 18 20 20
bys_sum(by = x$group, val = x$value)
#>  [1] 54 54 51 54 51 51 51 54 51 51
bys_prod(by = x$group, val = x$value)
#>  [1] 29484 29484 16320 29484 16320 16320 16320 29484 16320 16320
bys_cummin(by = x$group, val = x$value)
#>  [1] 13 13 20  9  2  1  1  9  1  1
bys_cummax(by = x$group, val = x$value)
#>  [1] 13 14 20 14 20 20 20 18 20 20
bys_cumsum(by = x$group, val = x$value)
#>  [1] 13 27 20 36 22 23 31 54 34 51
bys_cumprod(by = x$group, val = x$value)
#>  [1]    13   182    20  1638    40    40   320 29484   960 16320
bys_lag(by = x$group, val = x$value)
#>  [1] NA 13 NA 14 20  2  1  9  8  3
bys_lead(by = x$group, val = x$value)
#>  [1] 14  9  2 18  1  8  3 NA 17 NA