John-Henry
John-Henry

Reputation: 1807

Using a tidyeval column after join

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.

Upvotes: 0

Views: 59

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388982

You can select the columns that contains var in it and take rowMeans.

library(dplyr)

join_then_average <- function(df1, df2, var) {
  full_join(df1, df2, by = "id")  %>%
    mutate(x = rowMeans(select(., contains(var))))
}

join_then_average(a, b, 'x')

# A tibble: 3 x 4
#     id   x.x   x.y     x
#  <int> <int> <int> <dbl>
#1     1     4    16    10
#2     2     5    17    11
#3     3     6    18    12

Upvotes: 1

Related Questions