I have data similar to these.
v1 <- c("Fail", 20, 30, "Out", NA, 32, 33, 10)
v2 <- c(10, NA, NA, "Out", "Fail", 34, 35, 30)
df <- data.frame(v1,v2)
I need to transform this data frame as well. So that the 'words'
or NA
are the information immediately preceding, or if there is no previous information, I need to pick up the information later.
How can I actually do this using modern programming in R
? I'm doing something like this, according to this link.
df <- df %>% mutate(v11 = ifelse(v1 %in% "Fail", lag(),
ifelse(v1 %in% "Out", lag()),
ifelse(is.na(v1) %in% lag(), v1)))