Create new variable based on size of value in other column

793 views Asked by At

I am attempting to create a df with a new variable called 'epi' (stands for episode)... which is based on the 'days.since.last' variable. when the value of 'days.since.last' is greater than 90, I want the episode variable to increase by 1.

Here is the original df

   deid session.number days.since.last
1     1              1               0
2     1              2               7
3     1              3              12
4     5              1               0
5     5              2               7
6     5              3              14
7     5              4              93
8     5              5               5
9     5              6             102
10   12              1               0
11   12              2              21
12   12              3             104
13   12              4               4

Created from

help <- data.frame(deid = c(1, 1, 1, 5, 5, 5, 5, 5, 5, 12, 12, 12, 12),
                   session.number = c(1, 2, 3, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4),
                   days.since.last = c(0, 7, 12, 0, 7, 14, 93, 5, 102, 0, 21, 104, 4))

This is the output I am hoping to achieve

   deid session.number days.since.last epi
1     1              1               0   1
2     1              2               7   1
3     1              3              12   1
4     5              1               0   1
5     5              2               7   1
6     5              3              14   1
7     5              4              93   2
8     5              5               5   2
9     5              6             102   3
10   12              1               0   1
11   12              2              21   1
12   12              3             104   2
13   12              4               4   2

My best attempt is the below code, however, it does not change the first value of each new episode (they remain at 0)...

help$epi <- as.numeric(0)

tmp <- gapply(help, form = ~ deid, FUN = function(x)
{     
  spanSeq <- rle(x$days.since.last <= 90)$lengths[rle(x$days.since.last <= 90)$values == TRUE] 
  x$epi[x$days.since.last <= 90] <- rep(seq_along(spanSeq), times = spanSeq)
  rm(spanSeq)
  x    
})
help2 <- do.call("rbind", tmp)
rownames(help2)<-c(1:length(help2$deid))

Any assistance is greatly appreciated!

1

There are 1 answers

3
jalapic On BEST ANSWER

You could do this with dplyr like this:

library(dplyr)
help %>% group_by(deid) %>% mutate(epi = cumsum(ifelse(days.since.last>90,1,0))+1)


   deid session.number days.since.last epi
1     1              1               0   1
2     1              2               7   1
3     1              3              12   1
4     5              1               0   1
5     5              2               7   1
6     5              3              14   1
7     5              4              93   2
8     5              5               5   2
9     5              6             102   3
10   12              1               0   1
11   12              2              21   1
12   12              3             104   2
13   12              4               4   2

Essentially, the group_by does everything by group for your 'deid' variable. We assign a 1 or a 0 for each 'days.since.last' that is over 90. Then we create a new variable that is the cumulative sum of these 1's and 0's. By adding one to it we get your desired result.