rlocf

R insert week number from vector and perform na.locf afterwards


For a dataframe similar to below (but much larger obviously)) I want to add missing week numbers from a vector ( vector is named weeks below). In the end, each value for var1 should have 4 rows consisting of week 40 - 42 so the value inserted for week can be different for different values of var1. Initially the inserted rows can have value NA but as a second step I would like to perform na.locf for each value of var1. does anyone know how to do this?

Data frame example:

dat <- data.frame(var1 = rep(c('a','b','c','d'),3),
                  week = c(rep(40,4),rep(41,4),rep(42,4)),
                  value = c(2,3,3,2,4,5,5,6,8,9,10,10))
dat <- dat[-c(6,11), ]

weeks <- c(40:42)

Solution

  • Like this?

    dat %>% 
       tidyr::complete(var1,week) %>% 
       group_by(var1) %>% 
       arrange(week) %>% 
       tidyr::fill(value)
    # A tibble: 12 x 3
    # Groups:   var1 [4]
       var1   week value
       <fct> <dbl> <dbl>
     1 a        40     2
     2 a        41     4
     3 a        42     8
     4 b        40     3
     5 b        41     3
     6 b        42     9
     7 c        40     3
     8 c        41     5
     9 c        42     5
    10 d        40     2
    11 d        41     6
    12 d        42    10