Search code examples
raggregate

Convert monthly data to quarterly data using aggregate function


I have a little problem with using aggregate() function to convert monthly data to quarterly data. I searched a lot in this topic and there are a lot of examples how to use aggregate() function combined with ts() function. But is there any possibility that it would work for simple data frame? I mean let's take the code following :

date<-(seq(as.Date('2000-01-01'),as.Date('2020-04-01'),by = '1 month'))
x<-rnorm(244)
df1<-data.frame(date,x)
aggregate(df1,nfrequency=4,FUN=mean)

Error in aggregate.data.frame(df1, nfrequency = 4, FUN = mean) : argument "by" is missing, with no default

Can you help me with that?

UPDATE

I used the code proposed by Ronak.

The problem I'm dealing now is the following :

set.seed(20)
date<-(seq(as.Date('2000-01-01'),as.Date('2020-04-01'),by = '1 month'))
x<-rnorm(244)
df1<-data.frame(date,x)
unlist((df1 %>%   group_by(quarter = quarters(date), year = lubridate::year(date)) %>%   summarise(x = mean(x)) %>%   arrange(year))[,3])[1]
0.7874086 
mean(x[1],x[2],x[3])
1.162685

The means differ. Do you have any idea why?


Solution

  • You can extract quarter and year information from Date and aggregate :

    df2 <- aggregate(x~quarter + year, transform(df1, 
               quarter = quarters(date), year = as.integer(format(date, '%Y'))), mean)
    df2 <- df2[order(df2$year), ]
    

    Or using dplyr :

    library(dplyr)
    df1 %>%
      group_by(quarter = paste(quarters(date), lubridate::year(date))) %>%
      summarise(x = mean(x))
    
    #   quarter        x
    #   <chr>      <dbl>
    # 1 Q1 2000  0.347  
    # 2 Q1 2001 -0.592  
    # 3 Q1 2002  0.802  
    # 4 Q1 2003  0.237  
    # 5 Q1 2004 -0.00882
    # 6 Q1 2005  0.0535 
    # 7 Q1 2006  0.218  
    # 8 Q1 2007  0.177  
    # 9 Q1 2008 -0.258  
    #10 Q1 2009  0.246  
    # … with 72 more rows