[R] Reshape set operations?

hadley wickham h.wickham at gmail.com
Sun Aug 10 22:12:10 CEST 2008


Hi Kevin,

I think easiest way would be to create a single dataset with both
years in, and then work from that:

t2008$year <- 2008
t2007$year <- 2007

tall <- rbind(t2007, t2008)
mall <- melt(tall,
id.var=c("DayOfYear","Category","SubCategory","Sku", "year"),
measure.var=c("Quantity")

cast(mall, DayOfYear ~ variable | Sku, sum)
cast(mall, Year + DayOfYear ~ variable | Sku, sum)

Hadley


On Sat, Aug 9, 2008 at 11:44 AM,  <rkevinburton at charter.net> wrote:
> I have mange to use the library reshape to give me data structures that I want. Specifically:
>
> m2008 <- melt(t2008, id.var=c("DayOfYear","Category","SubCategory","Sku"), measure.var=c("Quantity"))
>  m2007 <- melt(t2007, id.var=c("DayOfYear","Category","SubCategory","Sku"), measure.var=c("Quantity"))
>
>  r2008 <- cast(m2008, DayOfYear ~ variable | Sku, sum)
>  r2007 <- cast(m2007, DayOfYear ~ variable | Sku, sum)
>
> Now I would like to union the two lists. So I start out with an empty master list that will contain (when I am done) the merge (union) of r2008 and r2007. By "union" I mean that if the Sku exists in r2007 and r2008 I would like to create a new data frame that has the lists for DayOfYear and Quantity "merged" and append it to 'master'. If the Sku is not common to both objects then just copy or append to the 'master'.
>
> Is it possible to come up with an expression and aggregate function that will do this? Is this better handle with aggregate functions or lapply? Being new to 'R' it is hard for me to tell.
>
> Thank you.
>
> Kevin
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
http://had.co.nz/



More information about the R-help mailing list