I use str_locate_all to get the start and end positions of a list of patterns in my string. It returns a list with the start and stop position for each match. How can I get the start and stop positions of all matches into separate lists?
library(stringr)
patterns <- c("ABS", "BSDF", "ERIDF", "RTZOP")
string <- "ABSBSDFERIDFRTZOPABSBSDFRTZOPABSBSDFERIDFRTZOP"
matches <- str_locate_all(string, patterns)
Result:
[[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
What I would like:
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)