I have a function that joins data together, and then should take the average of a column.
Here I can join the data, but I am not sure how to average the x.x
and x.y
columns in a sufficiently generalized way
library(dplyr)
a <- tibble(id = 1:3, x = 4:6)
b <- tibble(id = 1:3, x = 16:18)
join_then_average <- function(df1, df2, var) {
full_join(df1, df2, by = "id") # i want to average x.x, and x.y
}
join_then_average(a, b)
#> # A tibble: 3 x 3
#> id x.x x.y
#> <int> <int> <int>
#> 1 1 4 16
#> 2 2 5 17
#> 3 3 6 18
Conceptually I want to write something like:
mutate({{var}} := rowMeans(c({{var}}.x, {{var}}.y), na.rm = T)
but this doesn't work. I'm not sure the best way to approach this question.