#' @title Fast lead/lag for vectors
#' @description Find the "next" or "previous" values in a vector.
#' It has wrapped \pkg{data.table}'s \code{shift} function.
#' @param x A vector
#' @param n a positive integer of length 1,
#' giving the number of positions to lead or lag by. Default uses 1
#' @param fill Value to use for padding when the window goes beyond the input length.
#' Default uses \code{NA}
#' @return A vector
#' @seealso \code{\link[dplyr]{lead}},\code{\link[data.table]{shift}}
#' @examples
#' lead_dt(1:5)
#' lag_dt(1:5)
#' lead_dt(1:5,2)
#' lead_dt(1:5,n = 2,fill = 0)
#' @rdname lag_lead
#' @export
lead_dt = function(x,n = 1L,fill = NA){
shift(x,n,fill,type = "lead")
}
#' @rdname lag_lead
#' @export
lag_dt = function(x,n = 1L,fill = NA){
shift(x,n,fill,type = "lag")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.