manoj
manoj

Reputation: 71

R - aggregate daily to weekly with start date as Saturday

I have daily data and I want to convert to weekly with Week starting on Saturday.

 date value
1   11/5/2016    30
2   11/6/2016    20
3   11/7/2016    12
4   11/8/2016    22
5   11/9/2016    48
6  11/10/2016    50
7  11/11/2016    47
8  11/12/2016    12
9  11/13/2016    19
10 11/14/2016    31
11 11/15/2016    43
12 11/16/2016    26
13 11/17/2016    33
14 11/18/2016    36
15 11/19/2016    14
16 11/20/2016    15
17 11/21/2016    36
18 11/22/2016    38
19 11/23/2016    28
20 11/24/2016    21
21 11/25/2016    13    

I tried the following but it assumes Start of Week on Monday

data = as.xts(df$value,order.by=as.Date(df$date))
weekly = apply.weekly(data,sum)

I want the output to be aggregated by Saturday as Start Of Week.

Upvotes: 2

Views: 1647

Answers (1)

akrun
akrun

Reputation: 886938

The order.by statement in xts call is not with the correct format of Date class

data <- xts(df$value, order.by = as.Date(df$date, '%m/%d/%Y'))
tapply(data[,1], cumsum(format(index(data), '%w')==6), sum)

Upvotes: 2

Related Questions