[R] RESHAPE cast help.

rkevinburton at charter.net rkevinburton at charter.net
Wed Aug 6 09:22:52 CEST 2008


Thank you for your help.

Now let me really show my ignorance. How do I iterate through this data frame? So with your help I have a list like

head(c)
  DayOfYear              Category Quantity
1         1             (Unknown)       82
2         1 7" Plates   (Dessert)        4
3         1  7" Plates  (Dessert)       18
4         1  9" Plates   (Dinner)       10
5         1   9" Plates  (Dinner)        8
6         1  ACCESSORIES & MAKEUP      127

Now I want to form another data frame for each Category. So for the above "(Unknown)" I want to have a data.frame like:

DayOfYear Quantity
1                82
2                80
3               16

Where Quantity is obviously the quantity of "(Unknown)" category for the day of year.

For give my ignorance I have tried c[,"(Unknown)"], c$Category["(Unknown)"], etc and they all don't seem to work. There is something that I don't understand about the structure that is passed back from cast. Thanks again.

Kevin
---- hadley wickham <h.wickham at gmail.com> wrote: 
> On Tue, Aug 5, 2008 at 11:59 AM,  <rkevinburton at charter.net> wrote:
> > I have a set of data that is basically sales figures for a given year. It has columns for Yeaqr, Day Of Year, Sku, SubCatetory, and Category. The first few lines of data look like:
> >  Year DayOfYear    Sku Quantity CatId           Category       SubCategory
> > 1 2007         1 100091        1 10862            HOLIDAY         Christmas
> > 2 2007         1 100138        1 11160       PET COSTUMES Famous (Licensed)
> > 3 2007         1 100194        1 10749 HATS, WIGS & MASKS    Wigs - Women's
> > 4 2007         1 100432        1 10865            HOLIDAY            Easter
> > 5 2007         1 100911        1 10120                MEN   Superheroes Men
> >
> > So I have the following to help me summarize the data by the various columns:
> >
> > library("reshape")
> > t <- melt(t2007, id.var=c("DayOfYear","Category","SubCategory","Sku"), measure.var=c("Quantity"))
> >
> > The following seems to give me the sales for each day of the year:
> >
> > head(cast(t, DayOfYear ~ variable, sum))
> >  DayOfYear Quantity
> > 1         1      861
> > 2         2     1732
> > 3         3     2124
> > 4         4     1801
> > 5         5     2147
> > 6         6     1312
> >
> > Now I want to get the sales by day of year AND category. But the following doesnt seem right:
> >
> > head(cast(t, DayOfYear ~ Category ~ variable, sum))
> > [1] NA  2 NA NA  2 NA
> 
> Have a look at
> 
> cast(t, DayOfYear ~ Category ~ variable, sum)
> 
> it's a 3d array - probably not what you want.  You probably want
> 
> cast(t, DayOfYear + Category ~ variable, sum)
> 
> I'd suggest reading through the introduction to reshape pdf too.
> 
> Hadley
> 
> -- 
> http://had.co.nz/



More information about the R-help mailing list