Replace all missing values in the specified columns by the same value.
fill_by_value(x, ..., value = 0)
x | A data frame. |
---|---|
... | The unquoted column names of the variables that should be filled. |
value | The value to replace the missing values by. |
x
with the altered columns.
library(dplyr) # for the pipe operator x <- seq(as.Date('2016-01-01'), by = 'day', length.out = 366) x <- x[sample(1:366, 200)] %>% sort x_df <- data_frame(x = x, y1 = runif(200, 10, 20) %>% round, y2 = runif(200, 1, 50) %>% round, y3 = runif(200, 20, 40) %>% round, y4 = sample(letters[1:5], 200, replace = TRUE)) x_padded <- x_df %>% pad#>x_padded %>% fill_by_value(y1)#> # A tibble: 365 x 5 #> x y1 y2 y3 y4 #> <date> <dbl> <dbl> <dbl> <chr> #> 1 2016-01-01 19 4 22 d #> 2 2016-01-02 0 NA NA NA #> 3 2016-01-03 0 NA NA NA #> 4 2016-01-04 0 NA NA NA #> 5 2016-01-05 0 NA NA NA #> 6 2016-01-06 13 50 25 d #> 7 2016-01-07 19 22 25 d #> 8 2016-01-08 0 NA NA NA #> 9 2016-01-09 18 29 21 b #> 10 2016-01-10 15 41 24 c #> # … with 355 more rowsx_df %>% pad %>% fill_by_value(y1, y2, value = 42)#>#> # A tibble: 365 x 5 #> x y1 y2 y3 y4 #> <date> <dbl> <dbl> <dbl> <chr> #> 1 2016-01-01 19 4 22 d #> 2 2016-01-02 42 42 NA NA #> 3 2016-01-03 42 42 NA NA #> 4 2016-01-04 42 42 NA NA #> 5 2016-01-05 42 42 NA NA #> 6 2016-01-06 13 50 25 d #> 7 2016-01-07 19 22 25 d #> 8 2016-01-08 42 42 NA NA #> 9 2016-01-09 18 29 21 b #> 10 2016-01-10 15 41 24 c #> # … with 355 more rows