i have a data frame like this
set.seed(500)
df=data.frame(group=c(rep("A",20),rep("B",20),rep("C",20),rep("D",20)),value=round(runif(80,min=1,max=100)))
for each group i want to take the top value rows until their sum exceed/meet the target value
target=data.frame(group=c("A","B","C","D"),value=c(1000,400,500,300))
and output the new groups as 4 data frames.
I sorted them from biggest to smallest
df=df[with(df, order(group,-value)), ]
the desired output is
group value
a 98
a 93
...
a (sum from 98 to here, the group a subtotal should exceed 1000)
b 93
...
c 99
What's the best way of doing this?
Thanks.