I have a function that takes in two arguments (columns). The function changes the value of one column (x) based on what the value in another column is (y).
fun <- function(x, y) {
x = coalesce(case_when(y %in% c("hello", "hi") ~ '1',
y == "thanks" ~ '2'), x)
}
However, this needs to be done over many column pairs, why I want to make it a function.
Is this the right way of doing it:
df %>% mutate(across(c(col1, col3), c(col2, col4), fun))
from
col1 col2 col3 col4
1 1
2 4
5 "hello" 5 "hello"
3
4 4
5 "hi" 5 "thanks"
5 "thanks"
5 "goodbye" 5 "hello"
To
col1 col2 col3 col4
1 1
2 4
1 "hello" 1 "hello"
3
4 4
1 "hi" 2 "thanks"
2 "thanks"
5 "goodbye" 1 "hello"