data.table (version 1.12.4)

nafill: Fill missing values

Description

Fast fill missing values using constant value, last observation carried forward or next observation carried backward.

Usage

nafill(x, type=c("const","locf","nocb"), fill=NA,
       verbose=getOption("datatable.verbose"))
setnafill(x, type=c("const","locf","nocb"), fill=NA, cols=seq_along(x),
          verbose=getOption("datatable.verbose"))

Arguments

x

vector, list, data.frame or data.table of numeric columns.

type

character, one of "const", "locf" or "nocb". Defaults to "const".

fill

numeric or integer, value to be used to fill when type=="const".

cols

numeric or character vector specifying columns to be updated.

verbose

logical, TRUE turns on timing messages to the console.

Value

A list except when the input is a vector in which case a vector is returned. For setnafill the input argument is returned, updated by reference.

Details

Only double and integer data types are currently supported.

See Also

shift, data.table

Examples

Run this code
# NOT RUN {
x = 1:10
x[c(1:2, 5:6, 9:10)] = NA
nafill(x, "locf")

dt = data.table(v1=x, v2=shift(x)/2, v3=shift(x, -1L)/2)
nafill(dt, "nocb")

setnafill(dt, "locf", cols=c("v2","v3"))
dt
# }

Run the code above in your browser using DataCamp Workspace