[R] Reshape question.

rkevinburton at charter.net rkevinburton at charter.net
Wed Mar 11 20:47:03 CET 2009

This hopefully is trivial. I am trying to reshape the data using the reshape package.

First I read in the data:

a2009 <- read.csv("Total2009.dat", header = TRUE)

Then I trim it so that it only contains the columns that I have interested in:

m2009 <- melt(a2009, id.var=c("DayOfYear","Category","SubCategory","Sku"), measure.var=c("Quantity"), na.rm=TRUE)

Then I start to formulate the data that I will process:

c2009 <- cast(m2009, DayOfYear ~ variable | Category, sum)

Finally I aggregate the data:

t2009 <- cast(m2009, DayOfYear ~ variable, sum)

My question is on the third step above (repeated here)

c2009 <- cast(m2009, DayOfYear ~ variable | Category, sum)

This gets the data assocated with a unique 'Category' name. I want to get the data grouped by 'Category' and 'SubCategory'. The 'SubCategory' is not unique but the combination 'Category' and 'SubCategory' form a unique pair. What would be the formula that would give me the data grouped by Category AND SubCategory? Would it be as simple as:

c2009 <- cast(m2009, DayOfYear ~ variable | Category & SubCategory, sum)


Thank you for your suggestions.


More information about the R-help mailing list