【发布时间】:2021-10-20 16:38:23
【问题描述】:
我一直在努力理解tidyeval 以及quo、quos、sym、!!、!!! 等的使用。我做了一些尝试,但无法概括我的代码,因此它接受列向量并将文本处理应用于数据帧上的这些列。我的数据框如下所示:
ocupation tasks id
Sink Cleaner Cleaning the sink 1
Lion petter Pet the lions 2
我的代码如下所示:
stopwords_regex = paste(tm::stopwords('en'), collapse = '\\b|\\b')
stopwords_regex = glue('\\b{stopwords_regex}\\b')
df = df %>% mutate(ocupation_proc = ocupation %>% tolower() %>%
stringi::stri_trans_general("Latin-ASCII") %>%
str_remove_all(stopwords_regex) %>%
str_remove_all("[[:punct:]]") %>%
str_squish(),
tasks_proc = tasks %>% tolower() %>%
stringi::stri_trans_general("Latin-ASCII") %>%
str_remove_all(stopwords_regex) %>%
str_remove_all("[[:punct:]]") %>%
str_squish())
这带来了这样的东西:
ocupation tasks id ocupation_proc tasks_proc
Sink Cleaner Cleaning the sink 1 sink cleaner cleaning sink
Lion petter Pet the lions 2 lion petter pet lions
我想把它变成一个函数process_text_columns(df, columns_list, new_col_names),在这种情况下df=df、columns_list=c('ocupation', 'tasks')和new_col_names=c('ocupation_proc', 'tasks_proc'),(如果我可以做glue({colname}_proc)之类的事情,new_col_names可能甚至都没有必要了命名新列)。根据我收集到的信息,我需要使用across、sym、quos 和!!! 来概括该功能,但我尝试过的任何事情都失败了。你有什么想法吗?
谢谢
【问题讨论】:
-
它们应该足以制作一个可重复的示例。我唯一需要的是一个可以将 n 列名称作为参数并为其处理文本的函数,因此稍后我可以直接在另一个数据帧中使用它