I am trying to create a group variable based on cumulative sum of another variable. I want to apply a constraint on the cumulative sum if it goes beyond a limit (15000000) then group variable should change. Here is the code that I am working on:-
myDat = data.frame(Seg = c("A","B","C","D","F","G","H"),
Freq =c(4558848, 10926592, 15783936,8266496,7729349,13234562,9873456))
myDat$csum <- ceiling(ave(myDat$Freq,FUN=cumsum)/15000000)
# Seg Freq csum
# A 4558848 1
# B 10926592 2
# C 15783936 3
# D 8266496 3
# F 7729349 4
# G 13234562 5
# H 9873456 5
myDat1 <- aggregate(Freq~csum, data=myDat, FUN = sum)
# csum Freq
# 1 4558848
# 2 10926592
# 3 24050432
# 4 7729349
# 5 23108018
Some of the groups have gone beyond 15000000 limit. Can anyone help me with this code?
# Desired Results:-
# Seg Freq csum Desired csum
# A 4558848 1 1
# B 10926592 2 2
# C 15783936 3 3
# D 8266496 3 4
# F 6229349 4 4
# G 13234562 4 5
# H 9873456 5 6