Gavello
Gavello

Reputation: 1491

xts::apply.weekly thinks Monday is the last day of the week

I have an R data.frame containing one value for every quarter of hour

    Date                  A    B
 1  2015-11-02 00:00:00   0    0  //day start
 2  2015-11-02 00:15:00   0    0
 3  2015-11-02 00:30:00   0    0
 4  2015-11-02 00:45:00   0    0
 ...
 96 2015-11-02 23:45:00   0    0 //day end
 97 2015-11-03 00:00:00   0    0 //new day 
 ...
 6  2016-03-23 01:15:00   0    0 //last record

I use xts to construct a time series

xtsA <- xts(data$A,data$Date)

by using apply.daily I get the result I expect

apply.daily(xtsA, sum)    

    Date                   A
1   2015-11-02 23:45:00    400
2   2015-11-03 23:45:00    400
3   2015-11-04 23:45:00    500

but apply.weekly seems to use Monday as last day of the week

    Date                   A
19  2016-03-07 00:45:00    6500     //Monday
20  2016-03-14 00:45:00    5500     //Monday
21  2016-03-21 00:45:00    5000     //Monday

and I do not understand why it uses 00:45:00. Does anyone know?

Upvotes: 2

Views: 445

Answers (1)

Gavello
Gavello

Reputation: 1491

Data is imported from CSV file the Date column looks like this:

data <- read.csv("...", header=TRUE)

    Date            A
1   151102 0000     0
...

The error is in the date time interpretation and using

data$Date <- as.POSIXct(strptime(data$Date, "%y%m%d %H%M"), tz = "GMT") 

solves it, and now apply.weekly returns

     Date                   A
1    2015-11-08 23:45:00    3500 //Sunday
2    2015-11-15 23:45:00    4000 //Sunday
...

Upvotes: 1

Related Questions