Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group a data.frame by date?

Tags:

r

aggregate

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

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.

like image 287
Contango Avatar asked Feb 28 '13 18:02

Contango


2 Answers

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
like image 165
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 19 '22 15:10

A5C1D2H2I1M1N2O1R2T1


Use aggregate and paste0

> aggregate(Value ~ Date, data=DF, FUN=paste0 )
         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
like image 44
Jilber Urbina Avatar answered Oct 19 '22 14:10

Jilber Urbina