I have a data.table
object of dimension 421,570 x 16 which stores data for multiple stores. Stores can have different department. This is only a made up example. The dataset is bigger.
> head(raw.df)
Store Type StoreSize Dept Date Weekly_Sales
1: 1 A 151315 1 2010-02-05 24925
2: 1 A 151315 2 2010-02-05 46039
3: 2 A 152825 1 2010-02-05 41596
4: 2 A 152825 2 2010-02-05 19404
5: 3 B 110025 1 2010-02-05 21828
6: 3 B 110025 2 2010-02-05 21043
Ideally I want to create an array object which can store multiple matrix, on each matrix I want to have a single store. Basically I want to have on each level of the array the weekly sales on each department so I can run some time series analyses on the array object without running the same function multiple time.
This command will produce the kind of matrix I want for one layer of an array.
dcast(raw.df[which(raw.df$Store == 1), ], Date ~ Dept, value.var = "Weekly_Sales")
head(e1)
I was thinking to generate an empty array and fill it using a for loop
. I wrote this loop, which unfortunately doesn't work.
The dimension of the array are 143 (days), 99 (departments for each store), 45 (stores)
ts.a <- array(data = NA, dim = c(143, 99, 45))
for (i in 1:45) {
# generate 45 matrices, one for each store
paste("mat", i, sep = "") <- matrix(data = NA, nrow = 143, ncol = 99)
paste("mat", i, sep = "") <- dcast(raw.df[which(raw.df$Store == i), ], Date ~ Dept,
value.var = "Weekly_Sales")
# merge the matrix into the array object
}
I realise my approach might be completely wrong. My knowlegde or R and programming are completely self-taught.