Say I have the following dataframe:
ABC1_old <- c(1, 5, 3, 4, 3, NA, NA, NA, NA, NA)
ABC2_old <- c(4, 2, 1, 1, 5, NA, NA, NA, NA, NA)
ABC1_adj <- c(NA, NA, NA, NA, NA, 5, 5, 1, 2, 4)
ABC2_adj <- c(NA, NA, NA, NA, NA, 3, 2, 1, 4, 2)
df <- data.frame(ABC1_old, ABC2_old, ABC1_adj, ABC2_adj)
I want to create a column that compares each pair of ABCn_old
with its corresponding ABCn_adj
. (So ABC1_old
would be compared against ABCn_adj
, etc.) The resulting column would be called ABCn_new
. The evaluation would be that if ABCn_old
is NA
, fill in the blank with the corresponding value in ABCn_adj
, otherwise use ABCn_old
's value. The new columns would look like this:
df$ABC1_new <- c(1, 5, 3, 4, 3, 5, 5, 1, 2, 4)
df$ABC2_new <- c(4, 2, 1, 1, 5, 3, 2, 1, 4, 2)
I know a simple mutate
could work here, but I would like to use some kind of tidyverse
looping via purrr
if possible since the dataset is much larger in reality. Any ideas for the best way to achieve this?