R/custom_lag.R

Defines functions custom_lag

#' Lag a vector
#'
#' @param x Vector (of any class).
#' @param k Integer to specify the lag (defaults to 1).
#'
#' @return A lagged vector (not a time series as opposed to [stats::lag()]).
#' The behaviour of this `lag()` function is closer to that of `dplyr::lag()`.
#'
#' @examples
#' c(custom_lag(c(1,3,5), 1))
#'
#' c(custom_lag(LETTERS, 5))
#'
#' @noRd
#'
custom_lag <- function(x, k = 1) {

  c(rep_len(NA, k), x[seq_len(length(x) - k)])

}

Try the asymptor package in your browser

Any scripts or data that you put into this service are public.

asymptor documentation built on Oct. 5, 2022, 9:06 a.m.