我有一个数据框:
xxx <- data.frame(
time = c(0.001,0.002,0.003,0.308,0.309,0.310,0.311,0.405,0.406,0.407,0.408,0.201,0.202,0.405),
id = as.character(c(1,1,1,1,1,1,1,1,1,1,1,2,2,2)),
n = c(3,3,3,4,4,4,4,4,4,4,4,2,2,1),
x = c(10,12,16,3,5,9,11,51,56,59,80,1,2,9)
)
我想形成一个新变量(interval),以便: 对于每个id,每次开始一个新的间隔(时间),变量“interval”会改变该值。
time id n x interval
0.001 1 3 10 1
0.002 1 3 12 1
0.003 1 3 16 1
0.308 1 4 3 2
0.309 1 4 5 2
0.310 1 4 9 2
0.311 1 4 11 2
0.405 1 4 51 3
0.406 1 4 56 3
0.407 1 4 59 3
0.408 1 4 80 3
0.201 2 2 1 1
0.202 2 2 2 1
0.405 2 1 9 2
现在我想要为每个id选择包含min(x)的区间,以便:
time id n x interval
0.308 1 4 3 2
0.309 1 4 5 2
0.310 1 4 9 2
0.201 2 2 1 1
0.202 2 2 2 1
谢谢!
答案 0 :(得分:0)
我们可能需要一些门槛
library(data.table)
dt <- setDT(xxx)[, interval := cumsum(c(TRUE, diff(time) > 0.05)), id]
i1 <- dt[, .I[x < 10 & .N > 1], .(id, interval)]$V1
dt[i1]
# time id n x interval
#1: 0.308 1 4 3 2
#2: 0.309 1 4 5 2
#3: 0.310 1 4 9 2
#4: 0.201 2 2 1 1
#5: 0.202 2 2 2 1