【发布时间】:2019-05-19 02:06:26
【问题描述】:
考虑这个简单的例子
dfm1 <- tibble(text = c('hello world',
'hello quanteda')) %>%
corpus() %>% tokens() %>% dfm()
> dfm1
Document-feature matrix of: 2 documents, 3 features (33.3% sparse).
2 x 3 sparse Matrix of class "dfm"
features
docs hello world quanteda
text1 1 1 0
text2 1 0 1
和
dfm2 <- tibble(text = c('hello world',
'good nigth quanteda')) %>%
corpus() %>% tokens() %>% dfm()
Document-feature matrix of: 2 documents, 5 features (50.0% sparse).
2 x 5 sparse Matrix of class "dfm"
features
docs hello world good nigth quanteda
text1 1 1 0 0 0
text2 0 0 1 1 1
如您所见,我们在两个dfms 中具有相同的文本标识符:text1 和text2。
我想将dfm2“减去”到dfm1,以便将dfm1 中的每个条目减去dfm2 中的(可能)匹配条目(相同的文本,相同的词)
例如,text1、hello 出现 1 次,text2 也出现 1 次。因此,该条目的输出应为 0(即:1-1)。当然,dfms 中都没有的条目应该保持不变。
我如何在 quanteda 中做到这一点?
【问题讨论】:
-
您想从
dfm1从dfm2中减去匹配特征的计数吗?或者dfm2来自dfm1? -
顺序并不重要。我想在 dfms 中添加或减去匹配功能。这有意义吗?
标签: r sparse-matrix quanteda