Efficient group-by processing functions for data.table written with Rcpp.
See:
Replace NA by previous or next value, optionally by group
na_fill_by(dt, var = NULL, by = NULL, type = 1L, inplace = FALSE)
Argument | Description |
---|---|
dt |
a data.table |
var |
name(s) of variable(s) with atomic values ; if 'var' is omitted, all variables not in 'by' are selected |
by |
name(s) of variable(s) which determines groups (optional) ; if 'by' is ommitted, dt is considered as one group |
type |
specifies type of filling : 1 LOCF, 2 NOCB, 3 LOCF then NOCB. |
inplace |
when inplace = TRUE, na_fill is compute inplace |
a list with item for each var
When type = 1
, LOCF = Last Observation Carried Forward. Ex : c(NA, 1, NA, NA, 2, NA, 3, NA)
gives c(NA, 1, 1, 1, 2, 2, 3, 3)
When type = 2
, NOCB = Next Observation Carried Backward. Ex : c(NA, 1, NA, NA, 2, NA, 3, NA)
gives c(1, 1, 2, 2, 2, 3, 3, NA)
When type = 3
, LOCF then NOCB. Ex : c(NA, 1, NA, NA, 2, NA, 3, NA)
gives c(1, 1, 1, 1, 2, 2, 3, 3)
.
Replace NA by fill value
na_replace(dt, var = NULL, fill, inplace = FALSE)
Returns a logical TRUE for first row of each group
Returns a logical TRUE for last row of each group
Create a sequence 1...N by group
Case_when like function with multiple constant values
case_when_mult(...)
library(data.table)
dt <- data.table(a = c(1:5, NA, 7:10), b=rnorm(10), c=letters[1:10])
dt[, c("w", "x") :=
case_when_mult(
a %% 4 == 0 ~ list("p", 1.5),
a %% 2 == 0 ~ list("m", 4) ,
T ~ list("i", 6)
)
]
print(dt)
Returns first non NA value for var by group
Returns lag or lead values for var by group
Returns weighted sum for var by group
Returns TRUE if all boolean are TRUE by group
Returns TRUE if any boolean is TRUE by group