Aggregate Daily Data to Month/Year intervals

DatetimeR

Datetime Problem Overview


I don't often have to work with dates in R, but I imagine this is fairly easy. I have a column that represents a date in a dataframe. I simply want to create a new dataframe that summarizes a 2nd column by Month/Year using the date. What is the best approach?

I want a second dataframe so I can feed it to a plot.

Any help you can provide will be greatly appreciated!

EDIT: For reference:

> str(temp)
'data.frame':	215746 obs. of  2 variables:
 $ date  : POSIXct, format: "2011-02-01" "2011-02-01" "2011-02-01" ...
 $ amount: num  1.67 83.55 24.4 21.99 98.88 ...

> head(temp)
        date amount
1 2011-02-01  1.670
2 2011-02-01 83.550
3 2011-02-01 24.400
4 2011-02-01 21.990
5 2011-02-03 98.882
6 2011-02-03 24.900

Datetime Solutions


Solution 1 - Datetime

I'd do it with lubridate and plyr, rounding dates down to the nearest month to make them easier to plot:

library(lubridate)
df <- data.frame(
  date = today() + days(1:300),
  x = runif(300)
)
df$my <- floor_date(df$date, "month")

library(plyr)
ddply(df, "my", summarise, x = mean(x))

Solution 2 - Datetime

There is probably a more elegant solution, but splitting into months and years with strftime() and then aggregate()ing should do it. Then reassemble the date for plotting.

x <- as.POSIXct(c("2011-02-01", "2011-02-01", "2011-02-01"))
mo <- strftime(x, "%m")
yr <- strftime(x, "%Y")
amt <- runif(3)
dd <- data.frame(mo, yr, amt)

dd.agg <- aggregate(amt ~ mo + yr, dd, FUN = sum)
dd.agg$date <- as.POSIXct(paste(dd.agg$yr, dd.agg$mo, "01", sep = "-"))

Solution 3 - Datetime

A bit late to the game, but another option would be using data.table:

library(data.table)
setDT(temp)[, .(mn_amt = mean(amount)), by = .(yr = year(date), mon = months(date))]

# or if you want to apply the 'mean' function to several columns:
# setDT(temp)[, lapply(.SD, mean), by=.(year(date), month(date))]

this gives:

     yr      mon mn_amt
1: 2011 februari 42.610
2: 2011    maart 23.195
3: 2011    april 61.891

If you want names instead of numbers for the months, you can use:

setDT(temp)[, date := as.IDate(date)
            ][, .(mn_amt = mean(amount)), by = .(yr = year(date), mon = months(date))]

this gives:

     yr      mon mn_amt
1: 2011 februari 42.610
2: 2011    maart 23.195
3: 2011    april 61.891

As you see this will give the month names in your system language (which is Dutch in my case).


Or using a combination of lubridate and dplyr:

temp %>% 
  group_by(yr = year(date), mon = month(date)) %>% 
  summarise(mn_amt = mean(amount))

Used data:

# example data (modified the OP's data a bit)
temp <- structure(list(date = structure(1:6, .Label = c("2011-02-01", "2011-02-02", "2011-03-03", "2011-03-04", "2011-04-05", "2011-04-06"), class = "factor"), 
                       amount = c(1.67, 83.55, 24.4, 21.99, 98.882, 24.9)), 
                  .Names = c("date", "amount"), class = c("data.frame"), row.names = c(NA, -6L))

Solution 4 - Datetime

You can do it as:

short.date = strftime(temp$date, "%Y/%m")
aggr.stat = aggregate(temp$amount ~ short.date, FUN = sum)

Solution 5 - Datetime

Just use xts package for this.

library(xts)
ts <- xts(temp$amount, as.Date(temp$date, "%Y-%m-%d"))

# convert daily data
ts_m = apply.monthly(ts, FUN)
ts_y = apply.yearly(ts, FUN)
ts_q = apply.quarterly(ts, FUN)

where FUN is a function which you aggregate data with (for example sum)

Solution 6 - Datetime

I have a function monyr that I use for this kind of stuff:

monyr <- function(x)
{
    x <- as.POSIXlt(x)
    x$mday <- 1
    as.Date(x)
}

n <- as.Date(1:500, "1970-01-01")
nn <- monyr(n)

You can change the as.Date at the end to as.POSIXct to match the date format in your data. Summarising by month is then just a matter of using aggregate/by/etc.

Solution 7 - Datetime

Here's a dplyr option:

library(dplyr)

df %>% 
  mutate(date = as.Date(date)) %>% 
  mutate(ym = format(date, '%Y-%m')) %>% 
  group_by(ym) %>% 
  summarize(ym_mean = mean(x))�

Solution 8 - Datetime

One more solution:

 rowsum(temp$amount, format(temp$date,"%Y-%m"))

For plot you could use barplot:

barplot(t(rowsum(temp$amount, format(temp$date,"%Y-%m"))), las=2)

Solution 9 - Datetime

Also, given that your time series seem to be in xts format, you can aggregate your daily time series to a monthly time series using the mean function like this:

d2m <- function(x) {
  aggregate(x, format(as.Date(zoo::index(x)), "%Y-%m"), FUN=mean)
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBtibert3View Question on Stackoverflow
Solution 1 - DatetimehadleyView Answer on Stackoverflow
Solution 2 - DatetimekmmView Answer on Stackoverflow
Solution 3 - DatetimeJaapView Answer on Stackoverflow
Solution 4 - DatetimealperovichView Answer on Stackoverflow
Solution 5 - DatetimealperovichView Answer on Stackoverflow
Solution 6 - DatetimeHong OoiView Answer on Stackoverflow
Solution 7 - DatetimesbhaView Answer on Stackoverflow
Solution 8 - DatetimeMarekView Answer on Stackoverflow
Solution 9 - DatetimedospringView Answer on Stackoverflow