Turn implicit missing values into explicit missing values

fill_na(.data, ...)

Arguments

.data

A data frame.

...

A set of name-value pairs. The values will replace existing explicit missing values by variable, otherwise NA. The replacement values must be of the same type as the original one. If using a function to fill the NA, please make sure that na.rm = TRUE is switched on.

See also

case_na, tidyr::fill, tidyr::replace_na

Examples

harvest <- tsibble( year = c(2010, 2011, 2013, 2011, 2012, 2014), fruit = rep(c("kiwi", "cherry"), each = 3), kilo = sample(1:10, size = 6), key = id(fruit), index = year ) # leave NA as is fill_na(harvest)
#> # A tsibble: 10 x 3 [1YEAR] #> # Keys: fruit [2] #> year fruit kilo #> <dbl> <chr> <int> #> 1 2010 cherry NA #> 2 2010 kiwi 3 #> 3 2011 cherry 5 #> 4 2011 kiwi 7 #> 5 2012 cherry 9 #> 6 2012 kiwi NA #> 7 2013 cherry NA #> 8 2013 kiwi 4 #> 9 2014 cherry 1 #> 10 2014 kiwi NA
# replace NA with a specific value harvest %>% fill_na(kilo = 0L)
#> # A tsibble: 10 x 3 [1YEAR] #> # Keys: fruit [2] #> year fruit kilo #> <dbl> <chr> <int> #> 1 2010 cherry 0 #> 2 2010 kiwi 3 #> 3 2011 cherry 5 #> 4 2011 kiwi 7 #> 5 2012 cherry 9 #> 6 2012 kiwi 0 #> 7 2013 cherry 0 #> 8 2013 kiwi 4 #> 9 2014 cherry 1 #> 10 2014 kiwi 0
# replace NA using a function by variable # enable `na.rm = TRUE` when necessary harvest %>% fill_na(kilo = sum(kilo, na.rm = TRUE))
#> # A tsibble: 10 x 3 [1YEAR] #> # Keys: fruit [2] #> year fruit kilo #> <dbl> <chr> <int> #> 1 2010 cherry 29 #> 2 2010 kiwi 3 #> 3 2011 cherry 5 #> 4 2011 kiwi 7 #> 5 2012 cherry 9 #> 6 2012 kiwi 29 #> 7 2013 cherry 29 #> 8 2013 kiwi 4 #> 9 2014 cherry 1 #> 10 2014 kiwi 29
# replace NA using a function for each group harvest %>% group_by(fruit) %>% fill_na(kilo = sum(kilo, na.rm = TRUE))
#> # A tsibble: 10 x 3 [1YEAR] #> # Keys: fruit [2] #> # Groups: fruit [2] #> year fruit kilo #> <dbl> <chr> <int> #> 1 2010 cherry 15 #> 2 2011 cherry 5 #> 3 2012 cherry 9 #> 4 2013 cherry 15 #> 5 2014 cherry 1 #> 6 2010 kiwi 3 #> 7 2011 kiwi 7 #> 8 2012 kiwi 14 #> 9 2013 kiwi 4 #> 10 2014 kiwi 14
# replace NA pedestrian %>% group_by(Sensor) %>% fill_na( Date = lubridate::as_date(Date_Time), Time = lubridate::hour(Date_Time), Count = as.integer(median(Count, na.rm = TRUE)) )
#> # A tsibble: 70,176 x 5 [1HOUR] #> # Keys: Sensor [4] #> # Groups: Sensor [4] #> Sensor Date_Time Date Time Count #> <chr> <dttm> <date> <int> <int> #> 1 Birrarung Marr 2015-01-01 00:00:00 2015-01-01 0 1630 #> 2 Birrarung Marr 2015-01-01 01:00:00 2015-01-01 1 826 #> 3 Birrarung Marr 2015-01-01 02:00:00 2015-01-01 2 567 #> 4 Birrarung Marr 2015-01-01 03:00:00 2015-01-01 3 264 #> 5 Birrarung Marr 2015-01-01 04:00:00 2015-01-01 4 139 #> 6 Birrarung Marr 2015-01-01 05:00:00 2015-01-01 5 77 #> 7 Birrarung Marr 2015-01-01 06:00:00 2015-01-01 6 44 #> 8 Birrarung Marr 2015-01-01 07:00:00 2015-01-01 7 56 #> 9 Birrarung Marr 2015-01-01 08:00:00 2015-01-01 8 113 #> 10 Birrarung Marr 2015-01-01 09:00:00 2015-01-01 9 166 #> # ... with 70,166 more rows