tweet<- c("boy","girl","boy","x")
unique_words<- c("asdfdd","boy","girl","ahmed","asdf","asfeertrt")
word_count<-table(tweet[tweet %in%unique_words])
word_occurence<- as.integer(unique_words%in% tweet)
Run Code Online (Sandbox Code Playgroud)
我得到了这些输出:word_count ::
boy girl
2 1
Run Code Online (Sandbox Code Playgroud)
word_occurence ::
0 1 1 0 0 0
Run Code Online (Sandbox Code Playgroud)
但我希望输出如下:0 2 1 0 0 0
你可以这样做:
library(stringr)
rowSums(sapply(tweet, function(x, y) str_count(x, y), unique_words))
[1] 0 2 1 0 0 0
Run Code Online (Sandbox Code Playgroud)
该命令循环遍历tweet向量,计算每个出现的次数(str_count(); stringr包),然后使用rowSums对数据进行求和.
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |