通过更改 str_detect 来自动化脚本?
Automate script by changing str_detect?
我正在使用此脚本生成 table。在第二行中,对于 collectionName,我使用的是“Organization X”。我有许多不同的组织(Org Y、Org Z、.....)来创建这个 table。有没有办法自动化这个?并自动命名对象(当前为“orgx”)?
orgx <- df %>%
filter(str_detect(collectionName, c("Organization X"))) %>%
filter(str_detect(Year, paste(years, collapse = "|"))) %>%
corpus(text_field = "text") %>%
tokens(remove_punct = TRUE) %>%
tokens_select(stopwords('english'),selection='remove') %>%
tokens_tolower(keep_acronyms = FALSE) %>%
tokens_lookup(dictionary = dict, nomatch = TRUE) %>%
dfm() %>%
dfm_group(groups = "Title") %>%
dfm_weight(scheme = "prop") %>%
as.data.frame() %>%
mutate_at(vars(keyterms, true), funs(round(., 4)))
获取特定于该组织的列名称作为向量,通过循环遍历 map
中的向量和 return [=14] 中的输出,将其用作 str_detect
中的模式=]
library(dplyr)
library(purrr)
library(stringr)
vec <- c("Organization X", "Organization Y")
out <- map(vec, ~
df %>%
filter(str_detect(collectionName, .x)) %>%
filter(str_detect(Year, paste(years, collapse = "|"))) %>%
corpus(text_field = "text") %>%
tokens(remove_punct = TRUE) %>%
tokens_select(stopwords('english'),selection='remove') %>%
tokens_tolower(keep_acronyms = FALSE) %>%
tokens_lookup(dictionary = dict, nomatch = TRUE) %>%
dfm() %>%
dfm_group(groups = "Title") %>%
dfm_weight(scheme = "prop") %>%
as.data.frame() %>%
mutate_at(vars(keyterms, true), funs(round(., 4)))
)
names(out) <- sub("^(...).*\s+(\S)$", "\1\2", vec)
将输出保持在 list
中可能会更好。但是,如果我们需要将它分配给不同的对象,可以使用 list2env
或 assign
来完成
list2env(out, .GlobalEnv)
我正在使用此脚本生成 table。在第二行中,对于 collectionName,我使用的是“Organization X”。我有许多不同的组织(Org Y、Org Z、.....)来创建这个 table。有没有办法自动化这个?并自动命名对象(当前为“orgx”)?
orgx <- df %>%
filter(str_detect(collectionName, c("Organization X"))) %>%
filter(str_detect(Year, paste(years, collapse = "|"))) %>%
corpus(text_field = "text") %>%
tokens(remove_punct = TRUE) %>%
tokens_select(stopwords('english'),selection='remove') %>%
tokens_tolower(keep_acronyms = FALSE) %>%
tokens_lookup(dictionary = dict, nomatch = TRUE) %>%
dfm() %>%
dfm_group(groups = "Title") %>%
dfm_weight(scheme = "prop") %>%
as.data.frame() %>%
mutate_at(vars(keyterms, true), funs(round(., 4)))
获取特定于该组织的列名称作为向量,通过循环遍历 map
中的向量和 return [=14] 中的输出,将其用作 str_detect
中的模式=]
library(dplyr)
library(purrr)
library(stringr)
vec <- c("Organization X", "Organization Y")
out <- map(vec, ~
df %>%
filter(str_detect(collectionName, .x)) %>%
filter(str_detect(Year, paste(years, collapse = "|"))) %>%
corpus(text_field = "text") %>%
tokens(remove_punct = TRUE) %>%
tokens_select(stopwords('english'),selection='remove') %>%
tokens_tolower(keep_acronyms = FALSE) %>%
tokens_lookup(dictionary = dict, nomatch = TRUE) %>%
dfm() %>%
dfm_group(groups = "Title") %>%
dfm_weight(scheme = "prop") %>%
as.data.frame() %>%
mutate_at(vars(keyterms, true), funs(round(., 4)))
)
names(out) <- sub("^(...).*\s+(\S)$", "\1\2", vec)
将输出保持在 list
中可能会更好。但是,如果我们需要将它分配给不同的对象,可以使用 list2env
或 assign
list2env(out, .GlobalEnv)