Dear Jeff,
Try also

df=df[order(df$State, df$Gender), ]
df$cQuantity<-unlist(tapply(df[,3],df[,-3],cumsum))
df
  State Gender Quantity cQuantity
2    NY Female        2         2
4    NY Female        4         6
1    TX   Male        1         1
3    TX   Male        3         4


HTH,

Jorge



On Sun, Nov 16, 2008 at 9:12 PM, jeffc <hcen@andrew.cmu.edu> wrote:

>
> Hi,
>
> I have a data set similar to the following
>
> State   Gender  Quantity
> TX      Male    1
> NY      Female  2
> TX      Male    3
> NY      Female  4
>
>
> I need to calculate cumulative sum of the quantity by State and Gender. The
> expected output is
> State   Gender  Quantity        CumQuantity
> TX      Male    1       1
> TX      Male    3       4
> NY      Female  2       2
> NY      Female  4       6
>
> I highly appreciate if someone can give me some hints on solving that in R.
>
> Hao
>
> --
> View this message in context:
> http://www.nabble.com/how-to-calculate-another-vector-based-on-the-data-from-a-combination-of-two-factors-tp20532749p20532749.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

