NA
entries of a vector with the previous non-NA valueSometimes one has a vector with some gaps (NA
) values, which cause
problems for several numeric functions. This function fillNAgaps
fills
these missing values by inserting the last preceeding non-NA-value. Leading
NA values (at the start of the vector will not be modified). If the
argument firstBack = TRUE
, leading NA
-values are replaced by
the first non-NA value.
Trailing NAs are always replaced by the last previous NA-value.
fillNAgaps(x, firstBack = FALSE)
vector with NAs properly replaced
The vector where NA-values should be filled by repeating the last preceeding non-NA value
if TRUE
, leading NAs are replaced by the first non-NA
value in the vector, otherwise leading NAs are left untouched.
This code was taken from the R Cookbook: http://www.cookbook-r.com/Manipulating_data/Filling_in_NAs_with_last_non-NA_value/ LICENSE (from that page): The R code is freely available for use without any restrictions. In other words: you may reuse the R code for any purpose (and under any license).