statar (version 0.1.1)

setna: fill NA in place based on non missing observations

Description

fill NA in place based on non missing observations

Usage

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

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

Arguments

x
a data.table
...
Variables to fill in. Default to all non grouping variables. See the select documentation.
along_with
Numeric variable along which NAs should be filled. Default to last key. See the select documentation.
by
Groups within which gaps should be fill. Default to keys (or to keys minus last if along_with is unspecified). See the select documentation.
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
vars
Used to work around non-standard evaluation.

Examples

Run this code
library(data.table)
DT <- data.table(
 id    = c(1, 1, 1, 1, 2, 2),
 date  = c(1992, 1989, 1991, 1993, 1992, 1991),
 value = c(NA, NA, 3, NA, 3.2, 5.2)
)
DT1 <- copy(DT)
setkey(DT1, id, date)
DT2 <- copy(DT1)
DT3 <- copy(DT1)
setna(DT, value, along_with = date, by = id)
setna(DT1)
setna(DT2, value, rollends = TRUE)
setna(DT3, value, roll = "nearest")

Run the code above in your browser using DataCamp Workspace