Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get sum of values every 8 days by date in data frame in R

I don't often have to work with dates in R, but I imagine this is fairly easy. I have daily data as below for several years with some values and I want to get for each 8 days period the sum of related values.What is the best approach?

Any help you can provide will be greatly appreciated!

 str(temp)
'data.frame':648 obs. of  2 variables:
 $ Date : Factor w/ 648 levels "2001-03-24","2001-03-25",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ conv2: num  -3.93 -6.44 -5.48 -6.09 -7.46 ...

head(temp)
Date              amount
24/03/2001  -3.927020472
25/03/2001  -6.4427004
26/03/2001  -5.477592528
27/03/2001  -6.09462162
28/03/2001  -7.45666902
29/03/2001  -6.731540928
30/03/2001  -6.855206184
31/03/2001  -6.807210228
1/04/2001   -5.40278802

I tried to use aggregate function but for some reasons it doesn't work and it aggregates in wrong way:

z <- aggregate(amount ~ Date, timeSequence(from =as.Date("2001-03-24"),to =as.Date("2001-03-29"), by="day"),data=temp,FUN=sum)
like image 631
user1954153 Avatar asked Jan 07 '13 06:01

user1954153


1 Answers

I prefer the package xts for such manipulations.

  1. I read your data, as zoo objects. see the flexibility of format option.

    library(xts)
    ts.dat <- read.zoo(text ='Date              amount
    24/03/2001  -3.927020472
    25/03/2001  -6.4427004
    26/03/2001  -5.477592528
    27/03/2001  -6.09462162
    28/03/2001  -7.45666902
    29/03/2001  -6.731540928
    30/03/2001  -6.855206184
    31/03/2001  -6.807210228
    1/04/2001   -5.40278802',header=TRUE,format = '%d/%m/%Y')
    
  2. Then I extract the index of given period

    ep <- endpoints(ts.dat,'days',k=8)
    
  3. finally I apply my function to the time series at each index.

    period.apply(x=ts.dat,ep,FUN=sum )
    2001-03-29 2001-04-01 
    -36.13014  -19.06520 
    
like image 164
agstudy Avatar answered Sep 30 '22 21:09

agstudy