I would like to make use of the na.locf
to carry forward non-missing values for data frames where first observation may be zero.
Problem
dta <- data.frame(A = c(NA, NA, 1, 2, 4, 5, NA, NA, NA),
B = c(NA, 5, 4, 5, 8, 9, NA, NA, 100))
dta %>% mutate_all(.funs = funs(na.locf(.)))
Error in
mutate_impl(.data, dots)
: ColumnA
must be length 9 (the number of rows) or one, not 7
Desired results
Vectorize(require)(package = c("dplyr", "zoo"),
character.only = TRUE)
dta <- data.frame(A = c(0, NA, 1, 2, 4, 5, NA, NA, NA),
B = c(0, 5, 4, 5, 8, 9, NA, NA, 100))
dta %>% mutate_all(.funs = funs(na.locf(.)))
Workaround
The potential workaround would could involve replacing first set of NAs
with zeros and carrying zero forward that could be later replaced but I'm interested in leaving NAs where they are and exploring if there is a convenient way to make na.locf
ignore situations where the function did not receive non-NA value to start replacing.