子集数据框具有多个日期条件,范围介于 [英] Subsetting data frame with multiple date conditions for ranges in between
问题描述
我需要多个日期之间的子集.
数据框示例:
I need subsets between multiple dates.
Example data frame:
testdf <- data.frame(short_date = seq(as.Date("2007-03-01"),
as.Date("2008-09-01"), by = 'day'))
具有日期范围值的数据框示例:
An example of data frame with values for date ranges:
dates_cut <- structure(list(emergence = structure(c(13627, 13997), class = "Date"), disease_onset = structure(c(13694, 14062), class = "Date")), .Names = c("emergence", "disease_onset"), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))
显然,这只是一个示例,在很多年里,我需要介于( $ emergence date
和 $ disese_onset
)之间的数据子集.这适用于一个数据范围:
Obviously this is just a sample, there is a number of years for which I need subsets of data in between ($emergence date
and $disese_onset
).
This works for one data range:
testdf %>% filter(short_date >=dates_cut[1,1], short_date >=dates_cut[1,2])
问题是当有多个日期范围时.
The problem is when there are multiple date ranges.
谢谢.
推荐答案
一种选择是对 dates_cut
的行进行 lapply
,然后将每个子集存储在列表中.之后,您可以 rbind
将它们与 do.call
一起:
One option would be to lapply
over the rows of dates_cut
and then store each subset in a list. After that you can rbind
them all together with 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
这篇关于子集数据框具有多个日期条件,范围介于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!