Learn R Programming

LW1949 (version 1.1.0)

fill: Fill in Missing Values

Description

Fill in missing values in a vector, using the last recorded value.

Usage

fill(x, resetWhen = rep(FALSE, length(x)))

Arguments

x
A vector, can be character, numeric, or logical.
resetWhen
A logical vector, the same length as x, indicating elements that should not be filled in.

Value

A vector the same length as x, with all NAs or ""s replaced by the last value for the vector. Note that and missing values at the beginning of the vector will not be replaced.

Details

Similar to na.locf in the zoo package, but works for "" in character vectors as well.

Examples

Run this code
numvec <- c(NA, 1:5, NA, NA, NA, 10:12, NA)
newgroup <- c(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0)
fill(numvec)
fill(numvec, newgroup)

charvec <- c("", letters[1:5], "", "", "", letters[10:12], "")
fill(charvec)

Run the code above in your browser using DataLab