Is there a good way to carry the last observation of a row both forward and backwards n times? example vector, to demonstrate:
Before change:
vector <- c(NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, 2, NA, NA, NA, NA, NA, NA, 3, NA, NA, NA, NA)
After change, for n=2:
vector <- c(NA, NA, NA, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, NA, NA, 3, 3, 3, 3, 3, NA)
dplyr::fill()
doesn't seem to have a way to specify the number of filled rows, and zoo::na.locf()
has a locb option, but only if you do not specify the number of rows you would like filled.
If there is a way to do this such that the locb and locf could be specified to be two different values, eg, 1 and 3, that would be perfect for me. But if there's not an easy way to do that then just an locb and locf of a specified number of rows. Thanks for any help! I usually work in dplyr but will accept any sort of solution as this problem is really stumping me.