计算另一个向量中向量的出现

Ahm*_*afy 2 r data-analysis

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

Rom*_*man 5

你可以这样做:

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对数据进行求和.

  • 或者另一种选择是`colSums(sapply(unique_words,grepl,tweet))` (3认同)
  • 这可以用更紧凑的形式编写:`rowSums(sapply(tweet,str_count,unique_words))` (2认同)