View source: R/last_obs_carried_fwd.R
repeat_last | R Documentation |
Repeat the last non NA value. "Fill forward" NAs with the closest previous
non-NA value. There are lots of alternatives out there to this function. My
fave is tidyr::fill()
.
repeat_last(x, forward = TRUE, maxgap = Inf, na.rm = FALSE)
x |
A vector |
forward |
Logical; default is |
maxgap |
Numeric; By specifying maxgap, you can choose not to bridge overly long gaps. |
na.rm |
Logical; default is |
A similar vector to x
https://stackoverflow.com/questions/7735647/replacing-nas-with-latest-non-na-value
repeat_last(c(1,2,3,4,NA,NA))
repeat_last(c(1,NA,3,4,NA,NA), forward = FALSE)
repeat_last(c(1,NA,3,4,NA,NA, 5), forward = FALSE)
x = c(NA, NA, 1, NA, NA, NA, NA, NA, NA, 2, 3, 4, NA, NA, NA, NA, NA, 5, NA)
data.frame(x,
repeat_last(x),
repeat_last(x, forward = FALSE),
repeat_last(x, maxgap = 5),
check.names = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.