-3

Is it possible to calculate in r the number of days and count of b in var r from the following table:

id  r   s   t   u

1   a   100 1   27-06-2017

1   a   200 0   29-06-2017

1   b   300 0   01-07-2017

2   a   500 1   12-06-2017

3   b   100 0   02-07-2017

3   a   600 1   02-07-2017

4   a   200 0   12-06-2017

4   a   300 1   15-06-2017

4   b   200 0   18-06-2017

4   a   100 0   01-07-2017

5   a   200 0   04-06-2017       

grouped by unique ID where the condition = when r = b, sum of s >= sum of s when t = 1?

Julian_Hn
  • 1,581
  • 1
  • 5
  • 14
  • 3
    Your condition is unclear for me, can you please elaborate on that – Julian_Hn Apr 17 '19 at 10:00
  • i have to identify the number of days it took for sum of s's when column r has b to be >= sum of s's when the column t has 1 in it. also the count of those events when they reach that condition – ajaykumar cp Apr 17 '19 at 14:11
  • To give an example i want to know the number of days and times a person (ID) pays back money to me, wherein condition 1 in col t is where i give him money and b in col r is when he repays it to me. – ajaykumar cp Apr 17 '19 at 14:16
  • 1
    Your condition is still very unclear. How can r (a character) be greater than the sum of s (which is a numeric). And what do you mean by sum of s if we are only considering one row? – Julian_Hn Apr 17 '19 at 14:20
  • s is the money that is transferred between me and different people, so when i pay him column t is 1 and when they repay it is shown as b in column r. col t and r are flags. a in column r is the money which i transferred to them without expecting it back. So, I want to know how long and after how many repayments do they cover my payment – ajaykumar cp Apr 17 '19 at 14:45

1 Answers1

0

We can try

library(dplyr)
df1 %>%
   group_by(id) %>% 
   summarise(new =  sum((r == "b") & s >= sum(s[t == 1])))
akrun
  • 674,427
  • 24
  • 381
  • 486