Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum until a given value is reached

Tags:

r

if-statement

So I thought I had the answer to my question here: Cumulative sum until maximum reached, then repeat from zero in the next row but in fact it's not.

What I would like to do is be able to sum a column until a given value is reached in another column. If we take for instance:

Col1   Col2   Col3   
 0      12     
 0      14
 1      2
 2      0.5
 1      12
 4      3
 3      2

I'd like to be able to sum all the values of column 2 until we reach 4 in column 1. This would give here: 12+14+2+0.5+12

I'm completely new to R and I sincerely have no idea on how to proceed.

What I have is a data frame from a csv file:

mydata = read.csv("mycsv.csv")
like image 200
LBes Avatar asked Aug 20 '15 17:08

LBes


1 Answers

You can use cumsum(mydata$Col1 == 4) == 0 to get a logical vector of whether or not 4 has been reached in Col1. Then you can use simple indexing to grab the relevant elements from Col2:

sum(mydata$Col2[cumsum(mydata$Col1 == 4) == 0])
# [1] 40.5
like image 170
josliber Avatar answered Sep 19 '22 05:09

josliber