[R] how to calculate another vector based on the data from a combination of two factors

hadley wickham h.wickham at gmail.com
Mon Nov 17 05:05:23 CET 2008


On Sun, Nov 16, 2008 at 8:12 PM, jeffc <hcen at 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.

Here's one approach that uses the plyr package:

library(plyr)
ddply(df, .(State, Gender), transform, CumQuantity = cumsum(Quantity))

You can find out more about how this works at http://had.co.nz/plyr

Hadley

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



More information about the R-help mailing list