groupby_lagLead: Helpers for group_by |> mutate(leadOrLag)

groupby_lagR Documentation

Helpers for group_by |> mutate(leadOrLag)

Description

[Stable]

Cuts down on typing to preform relatively common functions.

Usage

groupby_lag(
  .data,
  ...,
  lagCol,
  newCol,
  n = 1L,
  order_by = NULL,
  default = NA,
  doUngroup = TRUE
)

groupby_lead(
  .data,
  ...,
  leadCol,
  newCol,
  n = 1L,
  order_by = NULL,
  default = NA,
  doUngroup = TRUE
)

Arguments

.data

data.frame/tibble

...

group_by fields past to group_by

lagCol, leadCol

what column in .data should be lagged or lead

newCol

name of column that the mutation creates

n

Positive integer of length 1, giving the number of positions to lead or lag by

order_by

Override the default ordering to use another vector or column

default

Value used for non-existant rows. Defaults to NA.

doUngroup

Ungroup the data? TRUE/FALSE. Default TRUE

Value

tibble

Examples

## Not run: 

df <- tibble::tribble(
  ~x,  ~y,  ~z,
  'A',  3,  90,
  'B',  3,  150,
  'B',  2,  123,
  'A',  1,  125,
  'C',  1,  111,
  'C',  2,  133,
  'B',  1,  121,
  'A',  2,  108,
  'C',  3,  125
)

groupby_lag(df, x, lagCol = z, newCol = "test", n = 1, order_by = y) %>%
  dplyr::arrange(x, y)
groupby_lag(df, x, lagCol = z, newCol = "test", n = 1) %>%
  dplyr::arrange(x)
groupby_lead(df, x, leadCol = z, newCol = "test", n = 1, order_by = y) %>%
  dplyr::arrange(x, y)
groupby_lead(df, x, leadCol = z, newCol = "test", n = 1) %>%
  dplyr::arrange(x)


## End(Not run)


MARC-KC/marcR documentation built on June 2, 2022, 9:31 p.m.