I am getting an error in console:
Error :=
({ : could not find function ":="
I am using a fuzzyjoin (by David Robinson) and tidyverse packages only. The function is accepted with no syntax errors. On execution the error is thrown at me. What could be the reason?
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)
Here is the example data
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"
)
Try using setNames
in by
to pass a named vector.
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)
}