I would like to replace values of a dataset with corrected values if those corrected are not NA.
df <- tibble(
id = c(1,2,3),
name = c("peter", "piper", "paul"),
alex.value = c("apple","banana","apple"),
alex.corrected = c("orange",NA,"banana"),
bob.value = c("monkey","lion","tiger"),
bob.corrected = c("lion","tiger", NA)
)
Desired output
df %>%
mutate(
alex = if_else(!is.na(alex.corrected), alex.corrected,
alex.value),
bob = if_else(!is.na(bob.corrected), bob.corrected,
bob.value),
)
I need to do this for many columns, so it would be great to have a solution that scales. I'm thinking it will involve REGEX and maybe purrr, something like
df %>%
map_df( str_detect(unique(*\\.)
but that is just a wild guess