在R中如何按日期拆分数据框 [英] In R how can I split a dataframe by date

查看:2351
本文介绍了在R中如何按日期拆分数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,其中一列是日期时间(chron)。我想把这个数据框拆分成只有日期部分的数据框列表。因此,每个数据帧将具有当天的所有数据。我看过分割函数,但不知道如何使用列值的一部分?

解决方案

说你有这个 data.frame

  df<  -  data.frame(date = rep .POSIXt(as.POSIXct(2010-01-01 15:26),by =day,length.out = 3),each = 3),var = rnorm(9))
> df
日期var
1 2010-01-01 15:26:00 -0.02814237
2 2010-01-01 15:26:00 -0.26924825
3 2010-01- 01 15:26:00 -0.57968310
4 2010-01-02 15:26:00 0.88089757
5 2010-01-02 15:26:00 -0.79954092
6 2010-01- 02 15:26:00 1.87145778
7 2010-01-03 15:26:00 0.93234835
8 2010-01-03 15:26:00 1.29130038
9 2010-01-03 15 :26:00 -1.09841234

按日需要拆分:

 > split(df,as.Date(df $ date))
$`2010-01-01`
date var
1 2010-01-01 15:26:00 -0.02814237
2 2010-01-01 15:26:00 -0.26924825
3 2010-01-01 15:26:00 -0.57968310

$`2010-01-02`
date var
4 2010-01-02 15:26:00 0.8808976
5 2010-01-02 15:26:00 -0.7995409
6 2010-01-02 15:26 :00 1.8714578

$`2010-01-03`
日期var
7 2010-01-03 15:26:00 0.9323484
8 2010-01- 03 15:26:00 1.2913004
9 2010-01-03 15:26:00 -1.0984123

编辑:



上述方法与 chron datetime对象一致:

  x<  -  chron(dates =02/27/92,times =22:29:56)
> x
[1](02/27/92 22:29:56)
> as.Date(x)
[1]1992-02-27

编辑2



确保 as.Date 更改您的数据至关重要,请看这里:

 #我正在使用DSTday使一个整个_apparent_ day的一个
x< ; - rep(seq.POSIXt(as.POSIXct(2010-03-27 00:31),by =DSTday,length.out = 3))
> x
[1]2010-03-27 00:31:00 GMT2010-03-28 00:31:00 GMT2010-03-29 00:31:00 BST
> as.Date(x)
[1]2010-03-272010-03-282010-03-28

第三个项目是夏季时间, as.Date 检索实际即减一个小时。为避免这种情况:

 > as.Date(cut(x,DSTday))
[1]2010-03-272010-03-282010-03-29


I have a dataframe where one column is a date time (chron). I would like to split this dataframe into a list of dataframes split by the date part only. So each dataframe will have all the data for that day. I looked at split function but not sure how to use part of a column value?

解决方案

say you have this data.frame :

    df <- data.frame(date=rep(seq.POSIXt(as.POSIXct("2010-01-01 15:26"), by="day", length.out=3), each=3), var=rnorm(9))
> df
                 date         var
1 2010-01-01 15:26:00 -0.02814237
2 2010-01-01 15:26:00 -0.26924825
3 2010-01-01 15:26:00 -0.57968310
4 2010-01-02 15:26:00  0.88089757
5 2010-01-02 15:26:00 -0.79954092
6 2010-01-02 15:26:00  1.87145778
7 2010-01-03 15:26:00  0.93234835
8 2010-01-03 15:26:00  1.29130038
9 2010-01-03 15:26:00 -1.09841234

to split by day you just need:

 > split(df, as.Date(df$date))
$`2010-01-01`
                 date         var
1 2010-01-01 15:26:00 -0.02814237
2 2010-01-01 15:26:00 -0.26924825
3 2010-01-01 15:26:00 -0.57968310

$`2010-01-02`
                 date        var
4 2010-01-02 15:26:00  0.8808976
5 2010-01-02 15:26:00 -0.7995409
6 2010-01-02 15:26:00  1.8714578

$`2010-01-03`
                 date        var
7 2010-01-03 15:26:00  0.9323484
8 2010-01-03 15:26:00  1.2913004
9 2010-01-03 15:26:00 -1.0984123

EDIT:

the above method is consistent with chron datetime object too:

x <- chron(dates = "02/27/92", times = "22:29:56")
> x
[1] (02/27/92 22:29:56)
> as.Date(x)
[1] "1992-02-27"

EDIT 2

making sure that as.Date doesn't change your data is crucial, see here:

# I'm using "DSTday" to make a sequece of one entire _apparent_ day
x <- rep(seq.POSIXt(as.POSIXct("2010-03-27 00:31"), by="DSTday", length.out=3))
> x
[1] "2010-03-27 00:31:00 GMT" "2010-03-28 00:31:00 GMT" "2010-03-29 00:31:00 BST"
> as.Date(x)
[1] "2010-03-27" "2010-03-28" "2010-03-28"

the third item is in the summer time and as.Date retrieve the actual day, i.e. minus one hour. To avoid this:

> as.Date(cut(x, "DSTday"))
[1] "2010-03-27" "2010-03-28" "2010-03-29"

这篇关于在R中如何按日期拆分数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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