【发布时间】:2015-12-11 20:59:01
【问题描述】:
我有两个向量:
- 文本向量
c('abc', 'asdf', 'werd', 'ffssd') - 模式向量
c('ab', 'd', 'w')
我想矢量化以下 for 循环:
for(p in 1 : length(patterns)){
count <- count + str_count(texts, p);
}
我使用了以下命令,但都不起作用。
> str_count(texts, patterns)
[1] 1 1 1 0
Warning message:
In stri_count_regex(string, pattern, opts_regex = attr(pattern, :
longer object length is not a multiple of shorter object length
> str_count(texts, t(patterns))
[1] 1 1 1 0
Warning message:
In stri_count_regex(string, pattern, opts_regex = attr(pattern, :
longer object length is not a multiple of shorter object length
我想要一个像这样的二维矩阵:
| patterns
------+--------
| 1 0 0
texts | 0 1 0
| 0 1 1
| 0 1 0
【问题讨论】:
-
matrix(stringr::str_count(rep(texts, length(patterns)), rep(patterns, each = length(texts))), length(texts), dimnames = list(texts, patterns))
标签: r vectorization