I have a function that applies specific functions to multiple columns in a data frame. Each of these functions are unique and can only be applied to that column.
convert_columns <- function(df) {
df %>% mutate(
a = convert_a(a),
b = convert_b(b),
c = convert_c(c),
d = convert_d(d),
e = convert_e(e)
)
}
However, it is possible that users may input a data frame that only have a subset of those columns (for example, only a, b, and c. I would like the function to mutate column a, b, and c if those columns exist in the inputted data frame and ignore columns d and e.
I have tried
convert_columns <- function(df) {
df %>% mutate(across(any of(),
a = convert_a(a),
b = convert_b(b),
c = convert_c(c),
d = convert_d(d),
e = convert_e(e)
))
}
and
convert_columns <- function(df) {
df %>% mutate(across(any of(
a = convert_a(a),
b = convert_b(b),
c = convert_c(c),
d = convert_d(d),
e = convert_e(e)
)))
}
These do not work. Is there a simple way in the tidyverse syntax to accomplish what I am trying to do? In my actual use case, I have ~150 columns I will be mutating.