I want to make function that takes this data
now changed before
"12ab" "yes" "21ba"
"34de" "no"
"56fg" "yes" "gf65"
"78hi" "no" NA
And turn it into
now changed before
"12ab" "yes" "21ba"
"34de" "no" "34de"
"56fg" "yes" "gf65"
"78hi" "no" "78hi"
So if before is empty, I want before to take the value of now (with the assumption that if it didn't change, it must have been the same.
I want to use a function as I want to apply it to more column pairs.
I tried this:
library(purrr)
library(dplyr)
fun <- function(data, x, y) {
coalesce(case_when(data[[y]] == NA | data[[y]] == '' ~ data[[x]], data[[y]])
}
df[c("before", "before1")] <- map2(c("now", "now1"),c("before", "before1") ~ fun(df, .x, .y))
But it doesn't do anything.