[R] RESHAPE cast help.

hadley wickham h.wickham at gmail.com
Tue Aug 5 23:54:57 CEST 2008


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