确定两个给定整数/行之间的整数/行

Determining the integers / lines between two given integers / lines

我在 r 中处理了大量的文本,我已经确定了包含关键文本的行号。我将它们存储为两个不同的对象:location1 和 location2

我现在想创建一个位于位置 1 和位置 2 之间的行的列表。

location1  int [1:393] 4 21 38 57 75 93 110 127 144 166 ...
location2  int [1:393] 6 23 41 59 77 95 112 129 147 168 ...

所以我正在寻找如下所示的输出:

5 22 39 40 58 76 111 128 145 146 167 

这比 location1 + 1 更复杂,因为在某些情况下 location1location2 之间有多条线。例如,当 location1 为 38 且 location2 为 41 时,我希望在输出中得到 39 和 40。

我该怎么做?

谢谢!

我们可以使用Map

unlist(Map(`:`, location1 + 1, location2 - 1))
#[1]   5  22  39  40  58  76  94 111 128 145 146 167

或者 Vectorized 方法 rep

v1 <- location2 - location1 -1
rep(location1, v1) + sequence(v1)
#[1]   5  22  39  40  58  76  94 111 128 145 146 167

或使用map2

library(purrr)
map2(location1 + 1, location2 - 1, `:`)

也可以改成

tmp <- sweep(df11 , 2,c(1, -1), `+`)

do.call(Map, c(f = `:`, unname(tmp)))

数据

location1 <- c(4, 21, 38, 57, 75, 93, 110, 127, 144, 166)

location2 <- c(6, 23, 41, 59, 77, 95, 112, 129, 147, 168)
df1 <- data.frame(location1, location2)
 unlist(Map(seq, location1 + 1, location2 - 1))
 [1]   5  22  39  40  58  76  94 111 128 145 146 167