【发布时间】:2021-02-06 04:06:01
【问题描述】:
我有一列来自 csv 文件的名为 text 的 50k 行推文(推文由句子、短语等组成)。我正在尝试计算该列中几个单词的频率。有没有比我在下面做的更简单的方法?
# Reading my file
tweets <- read.csv('coffee.csv', header=TRUE)
# Doing a grepl per word (This is hard because I need to look for many words one by one)
coffee <- grepl("coffee", text$tweets, ignore.case=TRUE)
mugs <- grepl("mugs", text$tweets, ignore.case=TRUE)
# Calculate the % of times among all tweets (This is hard because I need to calculate one by one)
sum(coffee) / nrow(text)
sum(starbucks) / nrow(text)
预期输出(假设我有超过 2 个单词)
Word Freq
coffee 50
mugs 40
cup 64
pen 12
【问题讨论】: