I am looking to create a new group based on two conditions. I want all of the cases until the cumulative sum of Value reaches 10 to be grouped together and I want this done within each person. I have managed to get it to work for each of the conditions separately, but not together using for loops and dplyr. However, I need both of these conditions to be applied. Below is what I would like the data to look like (I don't need an RunningSum_Value column, but I kept it in for clarification). Ideally I would like a dplyr solution, but I m not picky. Thank you in advance!
ID Value RunningSum_Value Group
PersonA 1 1 1
PersonA 3 4 1
PersonA 10 14 1
PersonA 3 3 2
PersonB 11 11 3
PersonB 12 12 4
PersonC 3 3 5
PersonD 4 4 6
PersonD 9 13 6
PersonD 5 5 7
PersonD 11 16 7
PersonD 6 6 8
PersonD 1 7 8
Here is my data:
df <- read.table(text="ID Value
PersonA 1
PersonA 3
PersonA 10
PersonA 3
PersonB 11
PersonB 12
PersonC 3
PersonD 4
PersonD 9
PersonD 5
PersonD 11
PersonD 6
PersonD 1", header=TRUE,stringsAsFactors=FALSE)