# [R] Sum objects in a column between an interval defined by conditions on another column

PIKAL Petr petr.pikal at precheza.cz
Thu May 2 11:44:15 CEST 2013

Hi

Just a question. Why you do not sum 5 with 3 and 4 or leave 3 alone and sum 4 and 5?

Basically combination of cut and aggregate gives you what you want, but tricky is how to do the cut operation based on your requirements and data

aggregate(df\$B, list(df\$A), sum)

aggregates B for any single value of A.

However I am not sure how to change A to set of levels based on your description. For this particular df it can be done by

aggregate(df\$B, list(cut(df\$A, c(0,1,2,4,6))), sum)

But it is not a general solution.

Petr

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Adriana Pitea
> Sent: Wednesday, May 01, 2013 3:42 PM
> To: r-help at r-project.org
> Subject: [R] Sum objects in a column between an interval defined by
> conditions on another column
>
> This comes as an application to this question:
> http://stackoverflow.com/questions/5896648/sum-object-in-a-column-
> between-an-interval-defined-by-another-
> column/5897695#comment23166107_5897695
>
> What I would like to know is how to adjust the answer if I want to sum
> the values in B, for ((A[i+1]-A[i]==0) or (A[i+1]-A[i]==1) or (A[i]-
> A[i-1]==0) or (A[i]-A[i-1]==1)) where i is the row index, so basically
> sum B rows for A-s that have the same value +/- 1, but not sum the same
> row twice?
> I tried building a loop function but I get stuck when using row indices
> with data frames. So if I have the next data frame:
>
>  df
>        A B
>  [1,]  1 4
>  [2,]  1 3
>  [3,]  3 5
>  [4,]  3 7
>  [5,]  4 3
>  [6,]  5 2
>
>
>
> The result I would obtain(considering that when I have ), would be:
>
>  df
>        A B
>  [1,]  1 7
>  [2,]  3 15
>  [3,]  5 2
>
>
> - Nanami
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help