假设我有两个数据表{
"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时间间隔[user
,start
],如果有的话。
在此示例中,期望的结果是
end
保留行号只是为了说明,时间顺序并不重要。
答案 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