Subsetting data.table由R中的日期范围设置 [英] Subsetting data.table set by date range in R
本文介绍了Subsetting data.table由R中的日期范围设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在日期范围内纳入data.table中的大型资料集。我的数据集如下所示:
I have a large dataset in data.table that I'd like to subset by a date range. My data set looks like this:
testset <- data.table(date=as.Date(c("2013-07-02","2013-08-03","2013-09-04",
"2013-10-05","2013-11-06")),
yr = c(2013,2013,2013,2013,2013),
mo = c(07,08,09,10,11),
da = c(02,03,04,05,06),
plant = LETTERS[1:5],
product = as.factor(letters[26:22]),
rating = runif(25))
我希望能够直接从 as.Date
列,而不使用 yr
, mo
或 da
列。目前,我属于 mo
,它有时非常笨重,特别是当年转换。
I'd like to be able to choose a date range directly from the as.Date
column without using the yr
, mo
, or da
columns. Currently, I'm subsetting by mo
and it's extremely clunky at times, especially when years switch over. A more elegant method of doing this would make my life infinitely easier.
提前感谢!
推荐答案
为什么不:
testset[date>="2013-08-02" & date<="2013-11-01"]
这篇关于Subsetting data.table由R中的日期范围设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文