我使用str_locate_all获取字符串中模式列表的开始和结束位置。它返回一个列表,其中包含每个比赛的开始和结束位置。如何将所有比赛的开始和结束位置放到单独的列表中?
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)
答案 0 :(得分:1)
在rbind中使用do.call将列表堆叠在一起,然后取出所需的列。
> 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