在自定义函数中使用 dplyr::left_join

Using dplyr::left_join inside a custom function

我想创建一个合并两个不同 table 的函数,使用每个数据集中的两个不同列。

df_1 <- data.frame(state_symbol = c('MG', 'SP', 'BA'),
                   city = c('Sao Paulo', 'Sao Paulo', 'Brumado'),
                   collected_data = c('red', 'green', 'blue'))

df_2 <- data.frame(S_Symb = c('MG', 'BA', "SP"),
                   municip = c('Sao Paulo', 'Brumado', 'Sao Paulo'),
                   population = c(123, 456, 789))

left_join(x = df_1,
          y = df_2,
          by = c("state_symbol" = "S_Symb",
                 "city" = "municip"))`

这会产生正确的结果:

  state_symbol      city collected_data population
1           MG Sao Paulo            red        123
2           SP Sao Paulo          green        789
3           BA   Brumado           blue        456

现在我想在一个函数中使用这段代码。我尝试了以下方法:

my_join <- function(tab_1,
                    tab_2,
                    df_1_city_col,
                    df_1_state_col,
                    df_2_city_col,
                    df_2_state_col) {
    
    output <- dplyr::left_join(x = tab_1,
                               y = tab_2,
                               by = c({{df_1_city_col}} = {{df_2_city_col}},
                                     {{df_1_state_col}} = {{df_2_state_col}}))
    return(output)
}

但这产生了以下错误:

> my_join <- function(tab_1,
+                     tab_2,
+                     df_1_city_col,
+                     df_1_state_col,
+                     df_2_city_col,
+                     df_2_state_col) {
+     
+     output <- dplyr::left_join(x = tab_1,
+                                y = tab_2,
+                                by = c({{df_1_city_col}} = {{df_2_city_col}},
Error: unexpected '=' in:
"                               y = tab_2,
                               by = c({{df_1_city_col}} ="

显然,函数不能很好地处理符号 c(something = something_else)。括号内的 = 对它来说太多了,无论我尝试什么,它都会立即产生错误。

仅供参考,我的函数调用如下所示:

my_join(tab_1 = df_1,
        tab_2 = df_2,
        df_1_city_col = 'city',
        df_1_state_col = 'state_symbol',
        df_2_city_col = 'municip',
        df_2_state_col = 'S_Symb')

我看到了其他一些类似的已回答问题,但找不到需要在每个输入的两列之间进行联接的问题 table。

如有任何帮助,我们将不胜感激。 谢谢。

请注意 c("state_symbol" = "S_Symb","city" = "municip") 实际上创建了一个命名向量,您可以使用 setNames 创建该向量以在函数中使用它。

my_join <- function(tab_1,
                    tab_2,
                    df_1_city_col,
                    df_1_state_col,
                    df_2_city_col,
                    df_2_state_col) {
  
  output <- dplyr::left_join(x = tab_1,
                             y = tab_2,
                             by = setNames(c(df_2_city_col, df_2_state_col),
                                            c(df_1_city_col, df_1_state_col)))
  return(output)
}

my_join(tab_1 = df_1,
        tab_2 = df_2,
        df_1_city_col = 'city',
        df_1_state_col = 'state_symbol',
        df_2_city_col = 'municip',
        df_2_state_col = 'S_Symb')

#  state_symbol      city collected_data population
#1           MG Sao Paulo            red        123
#2           SP Sao Paulo          green        789
#3           BA   Brumado           blue        456

在 base R 中,您可以使用 by.xby.y 指定要合并的列。

my_join <- function(tab_1,
                    tab_2,
                    df_1_city_col,
                    df_1_state_col,
                    df_2_city_col,
                    df_2_state_col) {
  
  output <- merge(tab_1, tab_2, 
                  by.x = c(df_1_city_col, df_1_state_col), 
                  by.y = c(df_2_city_col, df_2_state_col), 
                  all.x = TRUE)
  
  return(output)
}