Duck
Duck

Reputation: 39613

Remove rows in a data frame until the sum of a column get specific value

I am working in a data frame like this

c1 c2 c3 c4
1  2  1  10
1  1  1  50
1  2  2  40
2  1  1  30
3  3  3  80

I want to get a new data frame where the sum of values of c4 is between 60 and 100, in this case the sum is 210, then the new data frame must have a structure like this:

c1 c2 c3 c4
1  2  1  10
1  1  1  50
1  2  2  40

You can note that the sum of c4 is between 60 and 100, and the others raws had been deleted. How can I make this in R. Thanks.

Upvotes: 1

Views: 223

Answers (1)

Ferdinand.kraft
Ferdinand.kraft

Reputation: 12829

Use this:

DF[cumsum(DF$c4)<=100,]

Where DF is your dataframe.

Upvotes: 1

Related Questions