取消列出 str_locate_all 到单独的开始和结束列表

Unlist str_locate_all into separate start and end lists

我使用 str_locate_all 获取字符串中模式列表的开始和结束位置。它 returns 一个列表,其中包含每场比赛的开始和结束位置。如何将所有比赛的开始和结束位置放入单独的列表中?

library(stringr)

patterns <- c("ABS", "BSDF", "ERIDF", "RTZOP")
string <- "ABSBSDFERIDFRTZOPABSBSDFRTZOPABSBSDFERIDFRTZOP"

matches <- str_locate_all(string, patterns)

结果:

[[1]]
      start end
[1,]     1   3
[2,]    18  20
[3,]    30  32

[[2]]
       start end
[1,]     4   7
[2,]    21  24
[3,]    33  36

[[3]]
       start end
[1,]     8  12
[2,]    37  41

[[4]]
       start end
[1,]    13  17
[2,]    25  29
[3,]    42  46

我想要什么:

start <- c(1, 18, 30, 4, 21, 33, 8, 37, 13, 25, 42)
end <- c(3, 20, 32, 7, 24, 36, 12, 41, 17, 29, 46)

使用 do.call 和 rbind 将列表堆叠在一起,然后取出所需的列。

> library(stringr)
> 
> patterns <- c("ABS", "BSDF", "ERIDF", "RTZOP")
> string <- "ABSBSDFERIDFRTZOPABSBSDFRTZOPABSBSDFERIDFRTZOP"
> 
> matches <- str_locate_all(string, patterns)
> 
> all <- do.call(rbind, matches)
> start <- all[, 1]
> stop <- all[, 2]
> start
 [1]  1 18 30  4 21 33  8 37 13 25 42
> stop
 [1]  3 20 32  7 24 36 12 41 17 29 46