从R中范围的数据框中获取距离在特定值范围内的行

从R中范围的数据框中获取距离在特定值范围内的行,r,dataframe,R,Dataframe,如果我有一个范围数据框: start end 12 18 22 30 35 40 49 70 81 90 102 110 120 124 如何获取距离在R中某个上下限阈值内的行(即下一行的开始-上一行的结束在该阈值内) 比方说,我想得到距离在5-10之间的行,然后我想得到: start.1 end.1 start.2 end.2 22 30 35 40 35 40 49 70 102 110

如果我有一个范围数据框:

start end
12    18
22    30
35    40
49    70
81    90
102   110
120   124
如何获取距离在R中某个上下限阈值内的行(即下一行的开始-上一行的结束在该阈值内)

比方说,我想得到距离在5-10之间的行,然后我想得到:

start.1 end.1 start.2 end.2
22      30    35      40
35      40    49      70
102     110   120     124
这里,start.2-end.1始终在5-10之间。

库(dplyr)
library(dplyr)

df <- data.frame(
  start = c(12,22,35,49,81,102,120),
  end = c(18,30,40,70,90,110,124)
)

df %>%
  mutate(difference = start - lag(end),
         start.1 = lag(start),
         end.1 = lag(end),
         start.2 = start,
         end.2 = end) %>%
  filter(difference >= 5 & difference <= 10) %>%
  select(-c(difference, start, end))
df% 突变(差异=开始-滞后(结束), start.1=滞后(start), end.1=滞后(end), start.2=开始, 结束。2=结束)%>% 过滤器(差异>=5&差异% 选择(-c(差异、开始、结束))
使用基本R的单向

#Get the difference between consecutive start and end values
diffs <- df$start[-1] - df$end[-nrow(df)] 

#Get indices where the condition of difference is satisfied
rows_inds <- which(diffs >= 5 & diffs <= 10)

#cbind the rows present in row_inds and next row 
df1 <- cbind(df[rows_inds, ], df[rows_inds + 1, ])

#Make the columns name unique 
names(df1) <- make.unique(names(df1))

df1
#  start end start.1 end.1
#2    22  30      35    40
#3    35  40      49    70
#6   102 110     120   124
#获取连续开始值和结束值之间的差值

可能只有我一个人,但我不了解全部情况。起点和终点应该是随机数吗?(有确定的距离)