locf | R Documentation |
Fill NA values in a vector using previous non-NA value
locf(x, na = NA, cond = T, backfill = T)
x |
A vector of values that may have NA values to fill |
na |
A scalar or vector of values to treat as NA for purposes of filling |
cond |
A logical scalar or vector. NA will not be replaced if the corresponding condition is F. |
backfill |
A logical indicating if initial NA values should be backfilled from the first non-NA value. |
This is an interpolation method called Last Observation Carried Forward. It fills in a missing or NA value with the previous non-NA value. If backfill=TRUE (default), any initial NA values will be filled backward from the first non-NA value. Multiple values can be provided to consider as a match for NA.
The x vector with NA values replaced by previous non-NA value
x = c(1,"",2,3,"",".")
locf(x, na=c("","."))
locf(x, na=c("","."), cond=c(TRUE,TRUE,TRUE,TRUE,FALSE,TRUE))
locf(c(NA,NA,1,2,NA,NA,3))
locf(c(NA,NA,1,2,NA,NA,3), backfill=FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.