In a data.table
, I would like to fill forward previous NAs with the closest previous non-NA value, similar to this post
However, I would like to keep any NAs that occur after the non-NA value. The na.locf()
function seems to replace the NA’s afterwards. An example of what I’m trying to achieve:
Input data:
original_data <- data.frame(
V1 = c("Brain", NA, NA, NA, NA, NA),
V2 = c(NA, "Grey Matter", NA, NA, NA, NA),
V3 = c(NA, NA, "Cerebral Cortex", NA, NA, "Temporal Cortex"),
V4 = c(NA, NA, NA, "Cerebral cortex 1", "Cerebral cortex 2", NA),
stringsAsFactors = FALSE
)
original_data
Desired output:
desired_output <- data.frame(
V1 = c("Brain", "Brain", "Brain", "Brain", "Brain", "Brain"),
V2 = c(NA, "Grey Matter", "Grey Matter", "Grey Matter", "Grey Matter", "Grey Matter"),
V3 = c(NA, NA, "Cerebral Cortex", "Cerebral Cortex", "Cerebral Cortex", "Temporal Cortex"),
V4 = c(NA, NA, NA, "Cerebral cortex 1", "Cerebral cortex 2", NA),
stringsAsFactors = FALSE
)
desired_output
Using the na.locf()
function:
converted_data <- zoo::zoo(original_data)
converted_data <- zoo::na.locf(converted_data)
converted_data
Is there any way to preserve subsequent NAs?