我拥有的数据由组组成,每个组在时间维度上都有条目。
public @interface Inject
对于每个组,我想获得每个时间点的所有较早时间点。也就是说,结果应如下所示:
library(data.table); set.seed(42)
dat <- data.table(group=rep(1:2,each=5), t=rep(1:5,2), x=rnorm(10))
> head(dat)
group t x
1: 1 1 1.3709584
2: 1 2 -0.5646982
3: 1 3 0.3631284
4: 1 4 0.6328626
5: 1 5 0.4042683
6: 2 1 -0.1061245
其中> head(res)
group t.cons t x
1: 1 2 1 1.3709584
2: 1 3 1 1.3709584
3: 1 3 2 -0.5646982
4: 1 4 1 1.3709584
5: 1 4 2 -0.5646982
6: 1 4 3 0.3631284
给出了正在考虑的t.cons
中的条目。
答案 0 :(得分:3)
您可以使用非等价自我联接:
library(data.table)
set.seed(42)
dat <- data.table(group=rep(1:2,each=5), t=rep(1:5,2), x=rnorm(10))
# group t x
# 1: 1 1 1.37095845
# 2: 1 2 -0.56469817
# 3: 1 3 0.36312841
# 4: 1 4 0.63286260
# 5: 1 5 0.40426832
# 6: 2 1 -0.10612452
# 7: 2 2 1.51152200
# 8: 2 3 -0.09465904
# 9: 2 4 2.01842371
#10: 2 5 -0.06271410
dat[dat, .(group, t.cons = i.t, t = x.t, x),
on = c("group", "t < t"), allow.cartesian = TRUE,
nomatch = NULL]
# group t.cons t x
# 1: 1 2 1 1.37095845
# 2: 1 3 1 1.37095845
# 3: 1 3 2 -0.56469817
# 4: 1 4 1 1.37095845
# 5: 1 4 2 -0.56469817
# 6: 1 4 3 0.36312841
# 7: 1 5 1 1.37095845
# 8: 1 5 2 -0.56469817
# 9: 1 5 3 0.36312841
#10: 1 5 4 0.63286260
#11: 2 2 1 -0.10612452
#12: 2 3 1 -0.10612452
#13: 2 3 2 1.51152200
#14: 2 4 1 -0.10612452
#15: 2 4 2 1.51152200
#16: 2 4 3 -0.09465904
#17: 2 5 1 -0.10612452
#18: 2 5 2 1.51152200
#19: 2 5 3 -0.09465904
#20: 2 5 4 2.01842371