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

时间:2016-05-23 08:39:25

标签: r data-analysis

onView(R.id.parentLayout)
  .check(matches(allOf(
    withChild(allOf(withText("A"), isDisplayed())),
    withChild(allOf(withText("B"), isDisplayed())),
)));

我得到了这些输出:    word_count ::

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)

word_occurence ::

          boy girl 
           2    1

但我希望输出如下:   0 2 1 0 0 0

2 个答案:

答案 0 :(得分:5)

您可以执行以下操作:

library(stringr)
rowSums(sapply(tweet, function(x, y) str_count(x, y), unique_words))
[1] 0 2 1 0 0 0

该命令循环遍历tweet向量,计算每个出现的str_count(); stringr包,然后使用rowSums对数据进行求和。

答案 1 :(得分:3)

我们可以使用ifelse

ifelse(unique_words%in% tweet, word_count, 0)
#[1] 0 1 2 0 0 0