Curly curly passing a column name to mutate or regex_left_join returns 错误,找不到赋值运算符`:=`
Curly curly passing a column name to mutate or regex_left_join returns error, could not find assignment operator `:=`
我在控制台中遇到错误:
错误 :=
({ : 找不到函数 ":="
我只使用 fuzzyjoin(David Robinson 的)和 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 .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"
)
尝试在 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)
}
我在控制台中遇到错误:
错误 :=
({ : 找不到函数 ":="
我只使用 fuzzyjoin(David Robinson 的)和 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 .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"
)
尝试在 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)
}