R:基于另一个子集来设置数据表

时间:2015-12-07 14:45:44

标签: r datetime join data.table subset

假设我有两个数据表{ "rules": { "users": { "$uid": { // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid) ".write": "auth != null && auth.uid == $uid", ".read": "auth != null && auth.uid == $uid" } }, "snippets": { "$uid": { // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid) ".write": "auth != null && auth.uid == $uid", ".read": "auth != null && auth.uid == $uid" } }, "modules": { "$uid": { // grants write and read access to the owner of this user account whose uid must exactly match the key ($uid) ".write": "auth != null && auth.uid == $uid", ".read": "auth != null && auth.uid == $uid" } } } dm

dn

如何基于library(data.table) set.seed(12) dates = seq.Date(as.Date('2015-09-01'),as.Date('2015-11-01'), 2) dm = data.table(user=sample(LETTERS[1:4], 10, replace=T), time=sample(dates, 10)) dn = data.table(user=sample(LETTERS[1:8], 3, replace=F), start=c(as.Date('2015-09-01'), as.Date('2015-10-05'), as.Date('2015-09-14')), end=c(as.Date('2015-10-30'), as.Date('2015-11-01'), as.Date('2015-10-20'))) >dm # user time # 1: A 2015-09-25 # 2: D 2015-10-19 # 3: D 2015-09-21 # 4: B 2015-10-27 # 5: A 2015-09-15 # 6: A 2015-09-23 # 7: A 2015-10-21 # 8: C 2015-10-31 # 9: A 2015-10-01 # 10: A 2015-09-05 >dn # user start end # 1: B 2015-09-01 2015-10-30 # 2: F 2015-10-05 2015-11-01 # 3: A 2015-09-14 2015-10-20 的列来对dm进行分组?例如,对于dn中的每个用户,我们会查找dn匹配的dm,并将user的行放在time&#39之间。 ; s时间间隔[userstart],如果有的话。

在此示例中,期望的结果是

end

保留行号只是为了说明,时间顺序并不重要。

1 个答案:

答案 0 :(得分:3)

您可以尝试:

setkey(dm,user)
dm[dn][time>start & time<end]   
#   user       time      start        end
#1:    A 2015-09-25 2015-09-14 2015-10-20
#2:    A 2015-09-15 2015-09-14 2015-10-20
#3:    A 2015-09-23 2015-09-14 2015-10-20
#4:    A 2015-10-01 2015-09-14 2015-10-20
#5:    B 2015-10-27 2015-09-01 2015-10-30