Is there an elegant way to mutate a selection of columns in a dataframe (call it df1) based on the same selection of columns in another dataframe (df2) without doing a join?
The selection of columns in df2 have the same names as in df1, also both dataframes have the same number of rows (same id columns).
In this code, please replace 'elegant_function' with your elegant function. The selection of columns is 'a' and 'b'. The 'ignore_me' column is the id column in both, which might tempt you to join the dataframes, however please ignore it instead.
df1 <- data.frame(ignore_me = 1:5, a = 1:5, b = 11:15)
df2 <- data.frame(ignore_me = 1:5, a = c(0, 1, 1, 0, 2), b = c(1, 0, 1, 2, 0))
fn <- function(x1, x2){
if(x2 == 1){
return(x1 - x2)
}
if(x2 == 2){
return(x1 + x2)
}
x1
}
fn <- Vectorize(fn)
df <- elegant_function(
df1
, df2
, c("a", "b")
, fn
)
The output looks like this:
> df
ignore_me a b
1 1 1 10
2 2 1 12
3 3 2 12
4 4 4 16
5 5 7 15
Here is an example of an inelegant way to do this:
df <- df1 %>% select(ignore_me) %>%
mutate(
a = fn(df1$a, df2$a)
, b = fn(df1$b, df2$b)
)
Inelegant because each selected column requires a new line in the mutate function - it would be elegant if the selected columns could be provided as an input string to the function so it can vary at run time.
There may be other columns in df1, df2 to also ignore, I've only included the 'ignore_me' column as an example of these.
c("a", "b")column indf1anddf2how do you want to apply the function?