R匹配字符串模式中矩阵运算的矢量化

时间:2018-10-29 13:19:59

标签: r loops matrix vectorization

我正在使用下面的代码创建一个矩阵,该矩阵比较一个向量中的所有字符串以查看它们是否包含第二个向量中的任何模式:

strngs <- c("hello there", "welcome", "how are you")
pattern <- c("h", "e", "o")

M <- matrix(nrow = length(strngs), ncol = length(pattern))

for(i in 1:length(strngs)){
  for(j in 1:length(pattern)){
    M[i, j]<-str_count(strngs[i], pattern[j])
  }
}

M

它很好用,并返回我要寻找的矩阵:

      [,1] [,2] [,3]

[1,]    2    3    1

[2,]    0    2    1

[3,]    1    1    2

但是,我的真实数据集很大,并且像这样的循环无法很好地缩放到具有117、746、754个值的矩阵。有谁知道我可以矢量化或以其他方式加快它的方法?还是应该只学习C ++? ;)

谢谢!

3 个答案:

答案 0 :(得分:2)

您可以按照@snoram的建议使用outerstri_count_fixed

outer(strngs, pattern, stringi::stri_count_fixed)
#     [,1] [,2] [,3]
#[1,]    2    3    1
#[2,]    0    2    1
#[3,]    1    1    2

答案 1 :(得分:1)

通过删除内部循环并切换到stringi(建立在stringr上),在边缘上有一些改进。

M <- matrix(0L, nrow = length(strngs), ncol = length(pattern))
for(i in 1:length(strngs)) {
  M[i, ] <- stringi::stri_count_fixed(strngs[i], pattern)
}

然后是更标准的R方式:

t(sapply(strngs, stringi::stri_count_fixed, pattern))

答案 2 :(得分:1)

另一个解决方案,使用sapply。基本上是snoram's solution

t(sapply(strngs, stringi::stri_count_fixed, pattern))
#            [,1] [,2] [,3]
#hello there    2    3    1
#welcome        0    2    1
#how are you    1    1    2

测试。

由于总共有4种方法,所以这里有一些速度测试。

f0 <- function(){
  M<-matrix(nrow=length(strngs),ncol=length(pattern))
  for(i in 1:length(strngs)){
    for(j in 1:length(pattern)){
      M[i,j]<-stringr::str_count(strngs[i],pattern[j])
    }
  }
  M
}

f1 <- function(){
  M <- matrix(0L, nrow = length(strngs), ncol = length(pattern), )
  for(i in 1:length(strngs)) {
    M[i, ] <- stringi::stri_count_fixed(strngs[i], pattern)
  }
  M
}

f2 <- function() outer(strngs, pattern, stringi::stri_count_fixed)

f3 <- function() t(sapply(strngs, stringi::stri_count_fixed, pattern))

r0 <- f0()
r1 <- f1()
r2 <- f2()
r3 <- f3()

identical(r0, r1)
identical(r0, r2)
identical(r0, r3)  # FALSE, the return has rownames


library(microbenchmark)
library(ggplot2)

mb <- microbenchmark(
  op = f0(),
  snoram = f1(),
  markus = f2(),
  rui = f3()
)

mb
#Unit: microseconds
#   expr     min       lq      mean   median       uq     max
#     op 333.425 338.8705 348.23310 341.7700 345.8060 542.699
# snoram  47.923  50.8250  53.96677  54.8500  56.3870  69.903
# markus  27.502  29.8005  33.17537  34.3670  35.7490  54.095
#    rui  68.994  72.3020  76.77452  73.4845  77.1825 215.328

autoplot(mb)

enter image description here