[R] Groupwise sum

Jim Lemon jim at bitwrit.com.au
Thu May 22 00:44:34 CEST 2014


On Wed, 21 May 2014 03:59:26 PM Anna Carter wrote:
> Dear R forum,
> 
> I have following data.frame -
> 
> my_dat = data.frame(sr = c(0,0,0,0, 1, 1, 1, 1, 2, 2, 2, 2), bond = 
c("A",
> "B", "B", "B", "A", "B", "B", "B", "A", "B", "B", "B"), cashflow = c(1000,
> 2000, 2000, 4000, 10, 200, 300, 100, 80, 40, 60, 120))
> > my_dat
> 
>    sr   bond  cashflow
> 1   0    A     1000
> 2   0    B     2000
> 3   0    B     2000
> 4   0    B     4000
> 5   1    A       10
> 6   1    B      200
> 7   1    B      300
> 8   1    B      100
> 9   2    A       80
> 10  2    B       40
> 11  2    B       60
> 12  2    B      120
> 
> The above data.frame is just an example. My original data is bit 
large. My
> requirement is for given sr no, I need to add the cashflow values, for 
each
> bond. Thus, I need the output (as a data.frame) as -
> 
> 
> # OUTPUT
> 
>   sr bond cashflow_total
> 1  0    A           1000
> 2  0    B           8000
> 3  1    A             10
> 4  1    B            600
> 5  2    A             80
> 6  2    B            220
> 
> My - code
> 
> my_dat$key = paste(my_dat$bond, my_dat$sr, sep = "_")
> cashflow_total = tapply(my_dat$cashflow, my_dat$key, sum)
> 
> > cashflow_total
> 
>  A_0  A_1  A_2  B_0  B_1  B_2 
> 1000   10   80 8000  600  220
> 
> How do maintain the original order of sr and bond as in my_dat 
data.frame
> and obtain the 
> 
> above OUTPUT?
> 
Hi Anna,
Try this:

aggregate(my_dat$cashflow,my_dat[c("bond","sr")],sum)

Jim



More information about the R-help mailing list