如何按日期分组data.frame? [英] How to group a data.frame by date?
本文介绍了如何按日期分组data.frame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个.csv文件,格式如下:
I have a .csv file in the following format:
Date , Time , Value
1899-01-01 , 4:00:00 , 1
1899-01-01 , 4:01:00 , 2
1899-01-01 , 4:02:00 , 3
1899-01-01 , 4:03:00 , 4
1899-01-01 , 4:04:00 , 5
1900-08-22 , 22:00:00 , 101
1900-08-22 , 22:01:00 , 102
2013-08-29 , 4:00:00 , 1000
2013-02-29 , 4:02:00 , 1001
2013-02-29 , 4:03:00 , 1002
是否有可能按日期分组
以产生以下格式的 data.table
:
Is it possible to group by date
to produce a data.table
in the the following format:
Date , Vector(variable length)
1899-02-28, c(1,2,3,4,5)
1900-08-22, c(101,102)
1900-08-22, c(1000,1001,1002)
这是我到目前为止(经过一天的尝试之后)最好的:
This is the best that I have so far (after a day of attempts):
raw <- read.csv(pathName, header = TRUE, stringsAsFactors = FALSE)
groupedByDate <- split(raw, raw$Date)
但是,这似乎产生了一个非常宽的表格,每个日期都有一列,这与我的内容不太接近
However, this seems to produce a very wide table with one column for each date, which is not very close to what I want.
推荐答案
在<$ c $上使用聚合
怎么办? c> data.frame 名为 mydf,如下所示:
What about using aggregate
on a data.frame
named "mydf" as follows:
> temp <- aggregate(Value ~ Date, mydf, as.vector)
> temp
Date Value
1 1899-01-01 1, 2, 3, 4, 5
2 1900-08-22 101, 102
3 2013-02-29 1001, 1002
4 2013-08-29 1000
值列现在为列表
,其中包含您的向量。
The "Value" column is now a list
which contains your vectors.
> temp$Value
$`0`
[1] 1 2 3 4 5
$`1`
[1] 101 102
$`2`
[1] 1001 1002
$`3`
[1] 1000
使用分割
可能要查找的内容是:
What you were probably looking for with split
is:
> split(mydf$Value, mydf$Date)
$`1899-01-01 `
[1] 1 2 3 4 5
$`1900-08-22 `
[1] 101 102
$`2013-02-29 `
[1] 1001 1002
$`2013-08-29 `
[1] 1000
这篇关于如何按日期分组data.frame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文