I need to create stock data from a data frame. The data frame contains a begin and end date, which signify the period an item was in stock. I want to aggregate stock levels per item, then create a time series with the data.
I have data of the form:
A <- c("a","b","a","b","c")
begindate <- as.Date(c("2014-01-01", "2014-01-03", "2014-01-03", "2014-01-02", "2014-01-02"))
enddate <- as.Date(c("2014-01-04", "2014-01-05", "2014-01-06", "2014-01-04", "2014-01-06"))
source <- data.frame(A, begindate, enddate)
source
A begindate enddate
1 a 2014-01-01 2014-01-04
2 b 2014-01-03 2014-01-05
3 a 2014-01-03 2014-01-06
4 b 2014-01-02 2014-01-04
5 c 2014-01-02 2014-01-06
what I want to create from this data is a time series like
2014-01-01 2014-01-02 2014-01-03 2014-01-04 2014-01-05 2014-01-06
a 1 1 2 2 1 1
b 1 2 2 1
c 1 1 1 1 1
the original data is rather large, about 180k rows. what is an effective way to do this?
EDIT
The answer given by David Arenburg worked well
library(data.table)
library(reshape2)
setDT(mydata)[, indx := .I]
mydata <- mydata[, list(A = A, seq(begindate, enddate, by = 1)), by = indx]
but for my data it was rather slow. Adding an intermediate step significantly sped up the casting operation.
# intermediate step (pre-aggregation)
mydata_aggregated <- mydata[, list(number_cases = length(indx)), by = list(A, V2)]
# casting over the aggregated list
mydata_series <- dcast(mydata_aggregated, V2 ~ A, value.var = "number_cases") # note
# that I switched the rows and columns, since I found that its easier to pass this
# data to zoo or xts
# creating the zoo object
mydata_zoo <- zoo(mydata_series[,-1],mydata_series[,1])