按日期间隔索引数据帧

时间:2012-12-17 09:09:16

标签: r date indexing

我有一个包含一列的数据框 日期格式的数百个日期,例如:

as.Date(c("2011-08-13","2011-09-13","2010-06-12","2012-09-13","2010-09-13","2012-05-26","2012-07-20"))

现在我想只选择15.03 <15的行。日期&lt; 15.8(10月15日至10月15日之间的所有日期,无视当年)。有没有一种简单的方法来选择(索引)这样的 方式是什么?


我稍微修改了我接受的答案,如下所示:

a <- as.Date(c("2011-08-13","2011-09-13","2010-06-12","2012-09-13","2010-09-13","2012-05-26","2012-07-20"))
lower <- as.Date("03-15",format="%m-%d")
upper <- as.Date("08-15",format="%m-%d")
a[format(a,"%m-%d") < format(upper,"%m-%d") & format(a,"%m-%d") > format(lower,"%m-%d")]
[1] "2011-08-13" "2010-06-12" "2012-05-26" "2012-07-20"

1 个答案:

答案 0 :(得分:3)

base中,我们的想法是使用函数format

a <- as.Date(c("2011-08-13","2011-09-13","2010-06-12","2012-09-13","2010-09-13","2012-05-26","2012-07-20"))
lower <- as.Date("2012-03-15")
upper <- as.Date("2012-08-15")
a[format(a,"%m-%d") < format(upper,"%m-%d") & format(a,"%m-%d") > format(lower,"%m-%d")]
[1] "2011-08-13" "2010-06-12" "2012-05-26" "2012-07-20"