0

I have

 > df
   corona cons_week      n
   <fct>      <dbl>  <int>
 1 Normal         1      7
 2 Normal         2      4
 3 Normal         4      4
 4 Normal         5      6
            ...
245 C19          245     1
246 C19          247     3
247 C19          248     1
248 C19          249     1
249 C19          250     3
250 C19          251     3

cons_week is the number of consecutive weeks since COVID lockdown. I want to quantify number of surgeries per week:

df %>% #based on reduced data sample below
  group_by(corona, cons_week) %>%
  summarise(n = n())

Which gives

# A tibble: 80 x 3
# Groups:   corona [2]
   corona cons_week     n
   <fct>      <dbl> <int>
 1 Normal         1     1
 2 Normal         4     1
 3 Normal        14     2
 4 Normal        16     2
 5 Normal        17     1
 6 Normal        24     1
 7 Normal        25     1
 8 Normal        28     1
 9 Normal        30     1
10 Normal        31     1

As you can see, there are 0 cases in cons_week==2,3,5,etc.. I want to add 0 for those cons_week. I tried .drop = FALSE - did not solve it:

df %>% 
  group_by(corona, cons_week, .drop = FALSE) %>%
  summarise(n = n())

Adding 0 cases to df must take into account that cons_week be should kept in consecutive order and match the corresponding df$corona (either "Normal" or "C19").

Expected output

 > df
   corona cons_week      n
   <fct>      <dbl>  <int>
 1 Normal         1      7
 2 Normal         2      4
 3 Normal         3      0 #ADDED
 4 Normal         4      4
 5 Normal         5      6
            ...
245 C19          245     1
246 C19          246     0 #ADDED
247 C19          247     3
248 C19          248     1
249 C19          249     1
250 C19          250     3
251 C19          251     3

Data

        df <- structure(list(corona = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Normal", 
"C19"), class = "factor"), cons_week = c(1, 4, 14, 14, 16, 16, 
17, 24, 25, 28, 30, 31, 33, 33, 34, 37, 38, 38, 42, 42, 43, 45, 
45, 56, 60, 62, 66, 66, 66, 68, 69, 75, 76, 77, 79, 82, 82, 89, 
90, 91, 93, 97, 98, 98, 104, 105, 106, 107, 108, 109, 112, 113, 
121, 123, 128, 130, 133, 133, 142, 146, 151, 151, 154, 155, 157, 
158, 162, 165, 173, 179, 179, 181, 184, 186, 195, 195, 198, 207, 
209, 209, 210, 212, 214, 215, 215, 216, 222, 228, 230, 231, 231, 
233, 233, 238, 238, 240, 241, 242, 247, 247)), row.names = c(NA, 
-100L), class = "data.frame")
cmirian
  • 2,218
  • 2
  • 14
  • 40

0 Answers0