-3

So i have different visitors, each having come more than once.

From the visit dates i got the visit gaps for each patient.

I want to assign episodes for each visits of each patient depending on their gaps. For Gap=0 (ie 1st visit of any new patient), Episode=1. If Gap>20, Episode=previous episode+1, if Gap<=20, Episode=previous Episode. And the whole thing starts again for new patient. I want to do this without using loop in R , preferably in dplyr.

Data :

Df <- data.frame(Visitors = c("V1","V1","V1","V1","V1","V1","V1","V2","V2","V2","V2","V2","V2","V3"),
Gap=c(0,6,18,35,43,9,11,0,3,67,98,12,2,0))

This is the expected Table :

Visitors Gap Episodes
    V1     0    1
    V1     6    1
    V1    18    1
    V1    35    2
    V1    43    3
    V1     9    3
    V1    11    3
    V2     0    1
    V2     3    1
    V2    67    2
    V2    98    3
    V2    12    3
    V2     2    3
    V3     0    1

iod
  • 7,412
  • 2
  • 17
  • 36

1 Answers1

0

Here's a dplyr solution. After you break the vector Df into groups by visitor, it cumulatively counts how many gaps were bigger than 20.

require(dplyr)
Df %>% group_by(Visitors) %>% mutate(episode=cumsum(Gap>20)+1)
# A tibble: 14 x 3
# Groups:   Visitors [3]
   Visitors   Gap episode
   <fct>    <dbl>   <dbl>
 1 V1           0       1
 2 V1           6       1
 3 V1          18       1
 4 V1          35       2
 5 V1          43       3
 6 V1           9       3
 7 V1          11       3
 8 V2           0       1
 9 V2           3       1
10 V2          67       2
11 V2          98       3
12 V2          12       3
13 V2           2       3
14 V3           0       1

And here's a base R solution that does the same thing:

Df$ep<-unlist(sapply(split(Df, Df$Visitors),function(x) cumsum(x[,"Gap"]>20)+1))
iod
  • 7,412
  • 2
  • 17
  • 36
  • Wow. Didn't realise it was so easy..i spent the entire afternoon tearing my hair. And the worst part is i had used cumsum but wasn't working, so changed the approach n it got more complicated. But thank you so much for this z got the desired output. Thank u again! – Siddharth Basu Roy Jul 02 '19 at 14:07