【发布时间】:2019-07-19 19:14:53
【问题描述】:
我通常使用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] 之类的东西来按组获取计数,但我不确定过滤器是如何进入那里的。
感谢您的帮助!
【问题讨论】:
-
你可以试试
res[start <= endmonth & end >= startmonth, .N, by = .(startmonth, endmonth)] -
更正了我的示例并且您的方法有效。谢谢!
标签: r dplyr data.table