statar (version 0.1.1)

fill_gap: Add rows corresponding to gaps in some variable

Description

Add rows corresponding to gaps in some variable

Usage

fill_gap(x, ..., along_with = NULL, by = NULL, full = FALSE,
  roll = FALSE, rollends = if (roll == "nearest") c(TRUE, TRUE) else if
  (roll >= 0) c(FALSE, TRUE) else c(TRUE, FALSE), units = NULL)

fill_gap_(x, vars, along_with = NULL, by = NULL, full = FALSE, roll = FALSE, rollends = if (roll == "nearest") c(TRUE, TRUE) else if (roll >= 0) c(FALSE, TRUE) else c(TRUE, FALSE), units = NULL)

Arguments

x
A data.table
...
Variables to keep (beyond the by and along_with variable). Default to all variables. See the select documentation.
along_with
Numeric variable along which gaps should be filled. Default to last key. ee the select documentation.
by
Variables by which to group. Default to keys (or to keys minus last if along_with is unspecified). ee the select documentation.
full
A boolean. When full = FALSE (default) rows are filled with respect to min and max of ... within each group. When full = TRUE, rows are filled with respect to min and max of ... in the whole datasets.
roll
When roll is a positive number, this limits how far values are carried forward. roll=TRUE is equivalent to roll=+Inf. When roll is a negative number, values are rolled backwards; i.e., next observation carried backwards (NOCB). Use -Inf for unlimited roll
rollends
A logical vector length 2 (a single logical is recycled). When rolling forward (e.g. roll=TRUE) if a value is past the last observation within each group defined by the join columns, rollends[2]=TRUE will roll the last value forwards. rollends[1]=TRUE wil
units
Dreprecated
vars
Used to work around non-standard evaluation.

Examples

Run this code
library(data.table)
DT <- data.table(
    id    = c(1, 1, 1, 2),
    year  = c(1992, 1989, 1991, 1992),
    value = c(4.1, 4.5, 3.3, 3.2)
)
fill_gap(DT, value, along_with = year, by = id)
library(lubridate)
DT[, date:= mdy(c("03/01/1992", "04/03/1992", "07/15/1992", "08/21/1992"))]
DT[, datem :=  as.monthly(date)]
fill_gap(DT, value, along_with = datem , by = id)

Run the code above in your browser using DataLab