For each specified column in x replace the missing values by a function of the nonmissing values.

fill_by_function(x, ..., fun = mean)

Arguments

x

A data frame.

...

The unquoted column names of the variables that should be filled.

fun

The function to apply on the nonmissing values.

Value

x with the altered columns.

Examples

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)
#> Warning: `data_frame()` is deprecated, use `tibble()`. #> This warning is displayed once per session.
x_df %>% pad %>% fill_by_function(y1, y2)
#> pad applied on the interval: day
#> # A tibble: 363 x 3 #> x y1 y2 #> <date> <dbl> <dbl> #> 1 2016-01-03 14 44 #> 2 2016-01-04 11 34 #> 3 2016-01-05 11 20 #> 4 2016-01-06 15.0 26.4 #> 5 2016-01-07 15.0 26.4 #> 6 2016-01-08 15.0 26.4 #> 7 2016-01-09 15.0 26.4 #> 8 2016-01-10 19 28 #> 9 2016-01-11 12 13 #> 10 2016-01-12 17 11 #> # … with 353 more rows
x_df %>% pad %>% fill_by_function(y1, y2, fun = median)
#> pad applied on the interval: day
#> # A tibble: 363 x 3 #> x y1 y2 #> <date> <dbl> <dbl> #> 1 2016-01-03 14 44 #> 2 2016-01-04 11 34 #> 3 2016-01-05 11 20 #> 4 2016-01-06 15 27.5 #> 5 2016-01-07 15 27.5 #> 6 2016-01-08 15 27.5 #> 7 2016-01-09 15 27.5 #> 8 2016-01-10 19 28 #> 9 2016-01-11 12 13 #> 10 2016-01-12 17 11 #> # … with 353 more rows