These functions perform the standard between and within transformations on panel data.

within_i(
  .var,
  .df = get(".", envir = parent.frame()),
  .fcn = function(x) mean(x, na.rm = TRUE),
  .i = NULL,
  .t = NULL,
  .uniqcheck = FALSE
)

between_i(
  .var,
  .df = get(".", envir = parent.frame()),
  .fcn = function(x) mean(x, na.rm = TRUE),
  .i = NULL,
  .t = NULL,
  .uniqcheck = FALSE
)

Arguments

.var

Vector to be transformed

.df

Data frame, pibble, or tibble (usually the data frame or tibble that contains .var) which contains the panel structure variables either listed in .i and .t, or earlier declared with as_pibble(). If tlag is called inside of a dplyr verb, this can be omitted and the data will be picked up automatically.

.fcn

The function to be passed to dplyr::summarize(). x - .fcn(x) within .i is the within tranformation. .fcn(x) within .i minus .fcn overall is the between transformation. This will almost always be the default .fcn = function(x) mean(x,na.rm=TRUE).

.i

Quoted or unquoted variable(s) that identify the individual cases. Note that setting any one of .i, .t, or .d will override all three already applied to the data, and will return data that is as_pibble()d with all three, unless .setpanel=FALSE.

.t

Quoted or unquoted variable with the single variable name indicating the time. pmdplyr accepts two kinds of time variables: numeric variables where a fixed distance .d will take you from one observation to the next, or, if .d=0, any standard variable type with an order. Consider using the time_variable() function to create the necessary variable if your data uses a Date variable for time.

.uniqcheck

Logical parameter. Set to TRUE to always check whether .i and .t uniquely identify observations in the data. By default this is set to FALSE and the check is only performed once per session, and only if at least one of .i, .t, or .d is set.

Details

These functions do not take a .d argument because it is irrelevant here.

Examples

data(SPrail) # Calculate within- and between-route variation in price and add it to the data SPrail <- SPrail %>% dplyr::mutate( within_route = within_i(price, .i = c(origin, destination)), between_route = between_i(price, .i = c(origin, destination)) )