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

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

问题描述

我有这个数据:(12 月完成)

I have this data: (complete for December)

      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

并且需要对其进行子集/过滤,例如来自2014-12-05";到2014-12-25"

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

我知道您可以使用运算符:"创建一个序列.

I know 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 <- filter(Dates,日期(2014-12-05":2014-12-12")新日期"

Error: unexpected symbol in: "NewDate <- filter(Dates, date("2014-12-05":"2014-12-12") NewDate"

推荐答案

you can use subset

you could use subset

生成样本数据:

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 :

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天全站免登陆