R中2个数据帧之间的操作

时间:2015-11-08 21:42:54

标签: r text dataframe nlp

我有这个数据框:

 word       value
 very good    2 
 this is      2
 we are      -1
 very bad    -2
      . 
      .
      .

Sentences <- c('good good','very good very bad','we are we are','very good very good very bad')

如何制作一个取得&#39;得分的数据框?每个中心:

                         Sentences    score
    1                    good good        0
    2           very good very bad        0
    3                we are we are       -2
    4 very good very good very bad        2

1 个答案:

答案 0 :(得分:1)

你可以尝试:

library(stringr)
data.frame(Sentences, score=sapply(Sentences, function(x) sum(str_count(x, score$word)*score$value)), row.names = NULL)

                     Sentences score
1                    good good     0
2           very good very bad     0
3                we are we are    -2
4 very good very good very bad     2