[英]How to chain group_by, filter, distinct, count in data.table?
[英]dplyr into data.table: filter > group by > count
我通常使用dplyr
但面临相当大的数据集,而且我的方法非常慢。 我基本上需要按日期过滤一个df
组,并计算其中的出现次数
样本数据(已经将所有内容都转换为data.table
)
library(data.table)
library(dplyr)
set.seed(123)
df <- data.table(startmonth = seq(as.Date("2014-07-01"),as.Date("2014-11-01"),by="months"),
endmonth = seq(as.Date("2014-08-01"),as.Date("2014-12-01"),by="months")-1)
df2 <- data.table(id = sample(1:10, 5, replace = T),
start = sample(seq(as.Date("2014-07-01"),as.Date("2014-10-01"),by="days"),5),
end = df$startmonth + sample(10:90,5, replace = T)
)
#cross joining
res <- setkey(df2[,c(k=1,.SD)],k)[df[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL]
我的dplyr
方法有效,但速度慢
res %>% filter(start <=endmonth & end>= startmonth) %>%
group_by(startmonth,endmonth) %>%
summarise(countmonth=n())
我的data.table
知识有限,但我想我们会在日期列上设置setkeys()
,例如res[ , :=( COUNT = .N , IDX = 1:.N ) , by = startmonth, endmonth]
来获得按组计数,但我不确定过滤器如何进入该组。
感谢您的帮助!
您可以在联接中进行计数:
df2[df, on=.(start <= endmonth, end >= startmonth), allow.cartesian=TRUE, .N, by=.EACHI]
start end N
1: 2014-07-31 2014-07-01 1
2: 2014-08-31 2014-08-01 4
3: 2014-09-30 2014-09-01 5
4: 2014-10-31 2014-10-01 3
5: 2014-11-30 2014-11-01 3
或将其添加为df
的新列:
df[, n :=
df2[.SD, on=.(start <= endmonth, end >= startmonth), allow.cartesian=TRUE, .N, by=.EACHI]$N
]
这个怎么运作。 语法为x[i, on=, allow.cartesian=, j, by=.EACHI]
。 如果使用i
来查找x
值,则每一行。 符号.EACHI
表示将对i
每一行进行聚合( j=.N
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.