R:如何过滤/子集一系列日期 [英] R: How to filter/subset a sequence of dates

查看:176
本文介绍了R:如何过滤/子集一系列日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据:(完成Dizember)

i've this data: (complete for Dicember)

      date     sessions
1   2014-12-01  1932
2   2014-12-02  1828
3   2014-12-03  2349
4   2014-12-04  8192
5   2014-12-05  3188
6   2014-12-06  3277

需要对 12-05到2014-12-25

And a need to subet/filter this, for example from "2014-12-05" to "2014-12-25"

我现在可以使用运算符:创建一个序列。

I now that you can create a sequence with the operator ":".

示例:b< - c(1:5)

Example: b <- c(1:5)

但是如何过滤序列?我试过这个

But How to filter a sequence? I tried this

NewDate <- filter(Dates, date("2014-12-05":"2014-12-12"))

但是说:

错误:意外符号:
NewDate< - 过滤器(日期,日期(2014-12-05:2014-12-12)
NewDate

推荐答案

你可以使用子集

生成样本数据

temp<-
read.table(text="date     sessions
2014-12-01  1932
2014-12-02  1828
2014-12-03  2349
2014-12-04  8192
2014-12-05  3188
2014-12-06  3277", header=T)

确保这是日期格式:

temp$date <- as.Date(temp$date, format= "%Y-%m-%d")

temp



 #        date sessions
 # 1 2014-12-01     1932
 # 2 2014-12-02     1828
 # 3 2014-12-03     2349
 # 4 2014-12-04     8192
 # 5 2014-12-05     3188
 # 6 2014-12-06     3277

使用子集

subset(temp, date> "2014-12-03" & date < "2014-12-05")

其中:

  #        date sessions
  # 4 2014-12-04     8192

您还可以使用 []

temp[(temp$date> "2014-12-03" & temp$date < "2014-12-05"),]

这篇关于R:如何过滤/子集一系列日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆