I have a data table with may rows containing data about usage of some specific services. This data describes the usage of different service types in different regions:
type region quantity timestamp
small A 2 05/01/15
small B 1 05/01/15
big A 1 05/01/15
small A 2 06/01/15
small B 1 06/01/15
big A 3 06/01/15
...etc
I am performing some operations over the data series of every unique combination of type and region (each of these combinations produce its own time series so small-A
should be treated independently from small-B
, for example)
I already figured out how to do these kind of operations with aggregated data like this:
aggregatedDT <- DT[, .(quant = sum(quantity)), by = .(week, region,type)]
Now I need to save each data series into a separate CSV file. I am not sure if there are some built-in functionality to do such operation so I would like to know whether this is even possible.
My desired output would be:
small-A.csv:
week1: total quantity
week2: total quantity
...
And the same thing for small-B.csv, big-A.csv etc. Athe the moment, I have these data in one aggregated data.table but these csv files are meant as an input for another algorithm which needs to take the timeseries one by one.
You could try something like this to stay 'inside' data.table, while generating the appropriate filenames:
aggregatedDT[,write.csv(.SD,file=sprintf("%s-%s.csv", unique(type),unique(region))),
by=.(region,type)]
Data used:
aggregatedDT <- data.table(expand.grid(week=1:2, region=c("A","B"),type=c("big","small")),
quant=1:8)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments