【问题标题】:Curly curly passing a column name to mutate or regex_left_join returns error, could not find assignment operator `:=`Curly curly 将列名传递给 mutate 或 regex_left_join 返回错误,找不到赋值运算符`:=`
【发布时间】:2021-02-11 18:30:16
【问题描述】:

我在控制台中收到错误:

错误:=({ : 找不到函数":="

我只使用了一个模糊连接(大卫罗宾逊)和 tidyverse 包。该函数被接受,没有语法错误。在执行时,错误被抛出给我。可能是什么原因?

 df_recode_brand_name <- 
    tibble(
      regex_name  = c("wacoal|tempt", "calvin", "hanky", "victoria"),
      return_name = c("wacoal",       "calvin", "hanky", "victoria"))
  
  df_recode_product_category <- 
    tibble(
      regex_name  = c("lingerie", "pant", "bra", "undies"),
      return_name = c("lingerie", "pant", "bra", "undies"))
  
  
fn_regex_recode <- function(df, df_recode_dic, col_name) {
    df %>% regex_left_join(
      df_recode_dic, 
      by = c({{col_name}} := "regex_name"),
      ignore_case = T ) %>% 
    mutate({{col_name}} := if_else(is.na(return_name), str_to_lower({{col_name}}), return_name)) %>% 
    select(-regex_name, -return_name)  
  }

df_raw2 <-  
    df_raw %>%  
    fn_regex_recode(df_recode_brand_name,brand_name) %>% 
    fn_regex_recode(df_recode_product_category, product_category)

这是示例数据

df_raw <-
tibble::tribble(
                ~brand_name,                                  ~product_category,           ~description,
             "Calvin Klein",              "Women - Lingerie & Shapewear - Bras", "Practice bold, fearl",
                   "Wacoal",                                             "Bras", "Beautiful all over c",
        "Victoria's Secret",                           "Add-2-Cups Push-Up Bra", "The ultimate lift-lo",
                   "Wacoal",     "Women - Lingerie & Shapewear - Sexy Lingerie", "With luscious lace a",
        "Victoria's Secret",                 "Incredible by Victoria Sport Bra", "Tackle high-intensit",
             "Calvin Klein", "Women - Lingerie & Shapewear - Designer Lingerie", "Moderate coverage th",
        "Victoria's Secret",                        "Wicked Unlined Uplift Bra", "A little lift goes a",
        "Victoria's Secret",                     "Crochet Lace Cheekster Panty", "The prettiest croche",
        "Victoria's Secret",                           "Curved-hem Thong Panty",  "Seriously sleek and",
        "Victoria's Secret",                           "Add-2-Cups Push-Up Bra", "The ultimate lift-lo",
        "Victoria's Secret",                             "Perfect Coverage Bra", "Our fullest coverage",
               "US TOPSHOP",                                         "Lingerie", "Revamp your lingerie",
             "Calvin Klein",                                        "Sleepwear", "a modern cotton loun",
                    "AERIE",           "Everyday Loves Undies 7 for $27.50 USD", "Introducing Everyday",
      "b.tempt'd by Wacoal", "Women - Lingerie & Shapewear - Designer Lingerie",  "Sheer and sexy, the",
                   "Wacoal",              "Women - Lingerie & Shapewear - Bras", "Discover the glove-l",
   "Victoria's Secret Pink",                      "Wear Everywhere Push-Up Bra", "An everyday fave wit",
                   "WACOAL",                                          "PANTIES", "A sheer lace panty t",
        "Victoria's Secret",                          "Add-1?-Cups Push-Up Bra", "This push-up gives y",
                   "Wacoal",                                             "Bras", "Sport bra offers gre"
  )

【问题讨论】:

  • := 运算符来自data.table,而不是tidyverse
  • 不,tidyverse 包也使用它
  • 我相信替换是正确的,但是在您的c() 调用中仍然留下:=,这会导致问题。也许如果您使用诸如vctrs::vec_c() 之类的 tidyverse 函数,它会起作用。其他尝试 Ronak 的答案,您应该可以使用 {{}} 而不是替代品。

标签: r tidyverse tidyeval fuzzyjoin


【解决方案1】:

尝试在by 中使用setNames 来传递命名向量。

library(dplyr)
library(fuzzyjoin)
library(rlang)

  fn_regex_recode <- function(df, df_recode_dic, col_name) {
    val <- deparse(substitute(col_name))
    df %>% 
      regex_left_join(
        df_recode_dic, 
        by = setNames('regex_name', val),ignore_case = TRUE) %>%
      mutate({{col_name}} := if_else(is.na(return_name), str_to_lower({{col_name}}), return_name)) %>% 
      select(-regex_name, -return_name) 
  }

【讨论】:

  • 它抛出一个语法错误 Error: unexpected '}' in: " select(-regex_name, -return_name) }"
  • 你能提供df_raw的例子吗?
  • 感谢您的帮助。我已将数据样本附加到问题中。
  • 顺便说一句,我发现问题仅出在 regex_left_join 而不是 dplyr::mutate
  • deparse(substitute() 不健壮,它可能返回长度 > 1 的向量。as_label(substitute(arg))as_string(ensym(arg)) 没有这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 2019-12-29
相关资源
最近更新 更多