【发布时间】:2017-09-06 17:35:59
【问题描述】:
您好,我正在使用 tidy_text 格式,我正在尝试将字符串“emails”和“emailing”替换为“email”。
set.seed(123)
terms <- c("emails are nice", "emailing is fun", "computer freaks", "broken modem")
df <- data.frame(sentence = sample(terms, 100, replace = TRUE))
df
str(df)
df$sentence <- as.character(df$sentence)
tidy_df <- df %>%
unnest_tokens(word, sentence)
tidy_df %>%
count(word, sort = TRUE) %>%
filter( n > 20) %>%
mutate(word = reorder(word, n)) %>%
ggplot(aes(word, n)) +
geom_col() +
xlab(NULL) +
coord_flip()
这很好用,但是当我使用时:
tidy_df <- gsub("emailing", "email", tidy_df)
要替换单词并再次运行条形图,我收到以下错误消息:
UseMethod("group_by_") 中的错误: 'group_by_' 没有适用的方法应用于“字符”类的对象
有谁知道如何在不改变 tidy_text 的结构/类的情况下轻松替换整洁的文本格式中的单词?
【问题讨论】:
标签: r text-mining tidytext