Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group Dataframe hourly in R

I have a dataframe which has DateTime values in the date column and three columns with the counts for each date time.

I am trying to group the data hourly with the counts of the three columns

Sample Data

The aggregate function works for single columns but I am trying to do it for the entire data frame. Any tips?

aggregate(DateFreq$ColA,by=list((substr(DateFreq$Date,1,13))),sum) 
like image 841
ganeshran Avatar asked Jul 05 '13 10:07

ganeshran


2 Answers

You can use dplyr to do aggregation using dplyr::group_by and dplyr::summarise:

library(lubridate)
library(anytime)
library(tidyverse)

Lines <- "Date,c1,c2,c3
06/25/2013 12:01,0,1,1
06/25/2013 12:08,-1,1,1
06/25/2013 12:48,0,1,1
06/25/2013 12:58,0,1,1
06/25/2013 13:01,0,1,1
06/25/2013 13:08,0,1,1
06/25/2013 13:48,0,1,1
06/25/2013 13:58,0,1,1
06/25/2013 14:01,0,1,1
06/25/2013 14:08,0,1,1
06/25/2013 14:48,0,1,1
06/25/2013 14:58,0,1,1"

setClass("myDate")
setAs("character","myDate", function(from) anytime(from))
df <- read.csv(text = Lines, header=TRUE,  colClasses = c("myDate", "numeric", "numeric", "numeric"))

df %>%
  group_by(Date=floor_date(Date, "1 hour")) %>%
  summarize(c1=sum(c1), c2=sum(c2), c3=sum(c3))
# A tibble: 3 × 4
                 Date    c1    c2    c3
               <dttm> <dbl> <dbl> <dbl>
1 2013-06-25 12:00:00    -1     4     4
2 2013-06-25 13:00:00     0     4     4
3 2013-06-25 14:00:00     0     4     4
like image 185
RubenLaguna Avatar answered Oct 24 '22 09:10

RubenLaguna


You can use formula of the aggregate. But you should create correctly an hour variable before.

dat$hour <- as.POSIXlt(dat$Date)$hour
aggregate(.~hour,data=dat,sum)

here an example:

Lines <- "Date,c1,c2,c3
06/25/2013 12:01,0,1,1
06/25/2013 12:08,-1,1,1
06/25/2013 12:48,0,1,1
06/25/2013 12:58,0,1,1
06/25/2013 13:01,0,1,1
06/25/2013 13:08,0,1,1
06/25/2013 13:48,0,1,1
06/25/2013 13:58,0,1,1
06/25/2013 14:01,0,1,1
06/25/2013 14:08,0,1,1
06/25/2013 14:48,0,1,1
06/25/2013 14:58,0,1,1"

library(zoo)  ## better to read/manipulate time series
z <- read.zoo(text = Lines, header = TRUE, sep = ",",
              index=0:1,tz='',
              format = "%m/%d/%Y %H:%M")


dat <- data.frame(Date = index(z),coredata(z))
dat$hour <- as.POSIXlt(dat$Date)$hour
aggregate(.~hour,data=dat,sum)

hour       Date c1 c2 c3
1   12 5488624500 -1  4  4
2   13 5488638900  0  4  4
3   14 5488653300  0  4  4
like image 4
agstudy Avatar answered Oct 24 '22 10:10

agstudy