0

I have a dataset with drugs being administered overtime. I want to create groups for each block of the drug being administered. I figured out a simple method to do this with a for-loop that I can apply to each patients set of drugs.

BUT I am curious if there is simple way to do this within the realm of tidyverse? Not that it matters, but more so I am curious if there is a simple method already created for this problem.

Set-up

have <- tibble(
    patinet = c(1),
    date = seq(today(), today()+11,1),
    drug = c(rep("a",3), rep("b",3), rep("c",3), rep("a",3))
)


## Want

want <- tibble(
    patinet = c(1),
    date = seq(today(), today()+11,1),
    drug = c(rep("a",3), rep("b",3), rep("c",3), rep("a",3)),
    grp =  sort(rep(1:4,3))
)

> have
# A tibble: 12 × 3
   patinet date       drug 
     <dbl> <date>     <chr>
 1       1 2022-03-16 a    
 2       1 2022-03-17 a    
 3       1 2022-03-18 a    
 4       1 2022-03-19 b    
 5       1 2022-03-20 b    
 6       1 2022-03-21 b    
 7       1 2022-03-22 c    
 8       1 2022-03-23 c    
 9       1 2022-03-24 c    
10       1 2022-03-25 a    
11       1 2022-03-26 a    
12       1 2022-03-27 a    

> want
# A tibble: 12 × 4
   patinet date       drug    grp
     <dbl> <date>     <chr> <int>
 1       1 2022-03-16 a         1
 2       1 2022-03-17 a         1
 3       1 2022-03-18 a         1
 4       1 2022-03-19 b         2
 5       1 2022-03-20 b         2
 6       1 2022-03-21 b         2
 7       1 2022-03-22 c         3
 8       1 2022-03-23 c         3
 9       1 2022-03-24 c         3
10       1 2022-03-25 a         4
11       1 2022-03-26 a         4
12       1 2022-03-27 a         4
  • 1
    Does this answer your question? [How to create a consecutive group number](https://stackoverflow.com/questions/6112803/how-to-create-a-consecutive-group-number) – benson23 Mar 16 '22 at 12:44

1 Answers1

0

You can use data.table::rleid

have %>% mutate(group = data.table::rleid(drug))

# A tibble: 12 x 4
   patinet date       drug  group
     <dbl> <date>     <chr> <int>
 1       1 2022-03-16 a         1
 2       1 2022-03-17 a         1
 3       1 2022-03-18 a         1
 4       1 2022-03-19 b         2
 5       1 2022-03-20 b         2
 6       1 2022-03-21 b         2
 7       1 2022-03-22 c         3
 8       1 2022-03-23 c         3
 9       1 2022-03-24 c         3
10       1 2022-03-25 a         4
11       1 2022-03-26 a         4
12       1 2022-03-27 a         4
langtang
  • 22,248
  • 1
  • 12
  • 27