fill_by_value: Fill missing values by a single value

Description Usage Arguments Value Examples

View source: R/fill_functions.R

Description

Replace all missing values in the specified columns by the same value.

Usage

1
fill_by_value(x, ..., value = 0)

Arguments

x

A data frame.

...

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

value

The value to replace the missing values by.

Value

x with the altered columns.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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)
x_df %>% pad %>% fill_by_value(y1, y2, value = 42)

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

pad applied on the interval: day
# A tibble: 366 x 5
            x    y1    y2    y3    y4
       <date> <dbl> <dbl> <dbl> <chr>
 1 2016-01-01    14    28    28     a
 2 2016-01-02     0    NA    NA  <NA>
 3 2016-01-03    17    26    26     a
 4 2016-01-04     0    NA    NA  <NA>
 5 2016-01-05    15    23    22     b
 6 2016-01-06    14    49    39     a
 7 2016-01-07    10    23    25     c
 8 2016-01-08    15    45    28     e
 9 2016-01-09    19    37    22     c
10 2016-01-10    11    19    24     e
# ... with 356 more rows
pad applied on the interval: day
# A tibble: 366 x 5
            x    y1    y2    y3    y4
       <date> <dbl> <dbl> <dbl> <chr>
 1 2016-01-01    14    28    28     a
 2 2016-01-02    42    42    NA  <NA>
 3 2016-01-03    17    26    26     a
 4 2016-01-04    42    42    NA  <NA>
 5 2016-01-05    15    23    22     b
 6 2016-01-06    14    49    39     a
 7 2016-01-07    10    23    25     c
 8 2016-01-08    15    45    28     e
 9 2016-01-09    19    37    22     c
10 2016-01-10    11    19    24     e
# ... with 356 more rows

padr documentation built on Oct. 1, 2021, 5:07 p.m.