R 对于介于两者之间的范围,具有多个日期条件的子集数据框
我需要多个日期之间的子集。R 对于介于两者之间的范围,具有多个日期条件的子集数据框,r,date,R,Date,我需要多个日期之间的子集。 示例数据帧: testdf <- data.frame(short_date = seq(as.Date("2007-03-01"), as.Date("2008-09-01"), by = 'day')) 问题是当存在多个日期范围时 谢谢 一种选择是lappy在dates\u cut的行上,然后将每个子集存储在一个列表中。之后,您可以rbind将它们与do.call一起绑定: list &
示例数据帧:
testdf <- data.frame(short_date = seq(as.Date("2007-03-01"),
as.Date("2008-09-01"), by = 'day'))
问题是当存在多个日期范围时
谢谢 一种选择是
lappy
在dates\u cut
的行上,然后将每个子集存储在一个列表中。之后,您可以rbind
将它们与do.call一起绑定:
list <- lapply(1:nrow(dates_cut), function(i) {
testdf[which(testdf$short_date >= dates_cut[i, "emergence"] &
testdf$short_date <= dates_cut[i, "disease_onset"]), , drop = FALSE]})
res <- do.call(rbind, list)
head(res)
# short_date
#55 2007-04-24
#56 2007-04-25
#57 2007-04-26
#58 2007-04-27
#59 2007-04-28
#60 2007-04-29
list=dates\u cut[i,“出现”]&
testdf$short_date看看这个问题谢谢Lamia,应该看起来更好!谢谢,我已经找到了解决方案,感谢对我问题的评论,应该看起来更好,但你的解决方案似乎也不错。
testdf %>% filter(short_date >=dates_cut[1,1], short_date >=dates_cut[1,2])
list <- lapply(1:nrow(dates_cut), function(i) {
testdf[which(testdf$short_date >= dates_cut[i, "emergence"] &
testdf$short_date <= dates_cut[i, "disease_onset"]), , drop = FALSE]})
res <- do.call(rbind, list)
head(res)
# short_date
#55 2007-04-24
#56 2007-04-25
#57 2007-04-26
#58 2007-04-27
#59 2007-04-28
#60 2007-04-29