Reputation: 518
I need to create an Xts object from a dataframe, split by weeks and then perform calculations. I don't know how to match Xts POSIXct dates with the parent dataframe's POSIXct dates in order to obtain weekly calculations.
Example:
value <- rep(1, 10)
staff <- as.factor(c("Sam", "Sue", "Sam", "Mary", "Bob", "Sue", "Sam", "Sam", "Sue", "Mary"))
DTime <- as.POSIXct(c("2013-04-01 08:27:00", "2013-04-05 08:28:00", "2013-04-08 08:31:00",
"2013-04-11 08:32:00", "2013-04-15 08:33:00", "2013-04-15 08:40:00",
"2013-04-24 08:41:00", "2013-04-27 08:46:00", "2013-04-30 08:46:00",
"2013-04-30 08:46:00"))
DF <- data.frame(DTime, staff, value)
tapply(DF$value, DF$staff, sum)
That gives the monthly calculations but then splitting:
XtsDF <- xts(order.by=DF$DTime, dateFormat="POSIXct")
weeks <- split(XtsDF,f="weeks")
How do I take the date/time for each week and match to DF in order to perform the sum, by staff?
Upvotes: 2
Views: 944
Reputation: 121598
Here a data.table
solution, I would create first a new variable for weeks
library(data.table)
DT <- as.data.table(DF)
origin <- as.POSIXct("2012-12-9") ## A Sunday
DT[, c('weekID','sum') := c(weekID,sum(value)),
by=list(staff,weekID = as.numeric(DTime - origin) %/% 7)]
DTime staff value weekID sum
1: 2013-04-01 08:27:00 Sam 1 16 16
2: 2013-04-05 08:28:00 Sue 1 16 16
3: 2013-04-08 08:31:00 Sam 1 17 17
4: 2013-04-11 08:32:00 Mary 1 17 17
5: 2013-04-15 08:33:00 Bob 1 18 18
6: 2013-04-15 08:40:00 Sue 1 18 18
7: 2013-04-24 08:41:00 Sam 1 19 19
8: 2013-04-27 08:46:00 Sam 1 2 2
9: 2013-04-30 08:46:00 Sue 1 20 20
10: 2013-04-30 08:46:00 Mary 1 20 20
Upvotes: 1
Reputation: 18437
I think it will easier to use lubridate
which simplify a lot operation on POSIXt date and If you couple it to plyr
it's even more easier.
So here is my approach to this problem
require(lubridate)
require(plyr)
ddply(DF, .(month = month(DTime), staff), summarise, Sum = sum(value))
## month staff Sum
## 1 4 Bob 1
## 2 4 Mary 2
## 3 4 Sam 4
## 4 4 Sue 3
ddply(DF, .(week = week(DTime), staff), summarise, Sum = sum(value))
## week staff Sum
## 1 14 Sam 1
## 2 14 Sue 1
## 3 15 Mary 1
## 4 15 Sam 1
## 5 16 Bob 1
## 6 16 Sue 1
## 7 17 Sam 2
## 8 18 Mary 1
## 9 18 Sue 1
Upvotes: 3