I have a dataframe with old & new values. I need to update new values if something changed. I think I am really close, but I can't find the missing piece using tidyverse. With base R - using a for loop - it works, but I don't want to create new objects or overwrite the existing one.
data <- tribble(~id, ~firstname, ~lastname, ~old_firstname, ~old_lastname,
1, NA, NA, "Peter", "Busch",
2, NA, "Trochen-Pflaume", "Hans", "Trocken")
data%>%
mutate_at(vars(firstname, lastname), ~case_when(
is.na(.) & !is.na(str_c("old_",.)) ~ str_c("old_", .)),
!is.na(.) & . != str_c("old_",.) ~ .)
Basically, the only thing to check is whether the new value is empty, then the old value should be taken. As a result, more complex case_when queries are planned. But I fail to manipulate the column name within the mutate_at function.
What I want, but it depends on the case_when:
tribble(~id, ~firstname, ~lastname, ~old_firstname, ~old_lastname,
1, "Peter", "Busch", "Peter", "Busch",
2,"Hans", "Trochen-Pflaume", "Hans", "Trocken")
Thx for the help!