Replaces missing values with the given value, or optionally conditions the
replacement on the expressions given in ...
, which are applied to the
inputs in the same way as filter
.
if_na(.x, ..., then = default_value(.x), otherwise = NULL)
.x | Input vector of values |
---|---|
... | Additional expressions to condition replacement. NA values are only replaced when the additional expression matches. |
then | Replacement value. Defaults to a value based on the input type
and favors |
otherwise | If given, provides a secondary replacement value to be
subsituted for missing values when the conditions in |
library(dplyr)#> #>#>#> #>#>#> #>storms <- storms %>% filter(name == "Erika", year == 2003) %>% select(name, year, status, wind, contains("diameter")) # Default is replacement with appropriate default (0, FALSE, "", etc.) storms %>% mutate(hu_diameter = if_na(hu_diameter))#> # A tibble: 11 x 6 #> name year status wind ts_diameter hu_diameter #> <chr> <dbl> <chr> <int> <dbl> <dbl> #> 1 Erika 2003 tropical storm 35 NA 0 #> 2 Erika 2003 tropical storm 40 NA 0 #> 3 Erika 2003 tropical storm 40 NA 0 #> 4 Erika 2003 tropical storm 45 NA 0 #> 5 Erika 2003 tropical storm 50 NA 0 #> 6 Erika 2003 tropical storm 55 NA 0 #> 7 Erika 2003 tropical storm 60 NA 0 #> 8 Erika 2003 hurricane 65 NA 0 #> 9 Erika 2003 hurricane 65 NA 0 #> 10 Erika 2003 tropical storm 35 NA 0 #> 11 Erika 2003 tropical depression 25 NA 0# Replacement can be single value or vector of values storms %>% mutate( ts_diameter = if_na(ts_diameter, then = 30), hu_diameter = if_na(hu_diameter, then = rnorm(11, 100)) )#> # A tibble: 11 x 6 #> name year status wind ts_diameter hu_diameter #> <chr> <dbl> <chr> <int> <dbl> <dbl> #> 1 Erika 2003 tropical storm 35 30 98.6 #> 2 Erika 2003 tropical storm 40 30 100. #> 3 Erika 2003 tropical storm 40 30 97.6 #> 4 Erika 2003 tropical storm 45 30 100. #> 5 Erika 2003 tropical storm 50 30 101. #> 6 Erika 2003 tropical storm 55 30 101. #> 7 Erika 2003 tropical storm 60 30 98.2 #> 8 Erika 2003 hurricane 65 30 99.8 #> 9 Erika 2003 hurricane 65 30 99.8 #> 10 Erika 2003 tropical storm 35 30 99.7 #> 11 Erika 2003 tropical depression 25 30 99.4# Replacement can be predicated on other conditions in addition to missingness storms %>% mutate( hu_diameter = if_na(hu_diameter, then = 30, status == "hurricane") )#> # A tibble: 11 x 6 #> name year status wind ts_diameter hu_diameter #> <chr> <dbl> <chr> <int> <dbl> <dbl> #> 1 Erika 2003 tropical storm 35 NA NA #> 2 Erika 2003 tropical storm 40 NA NA #> 3 Erika 2003 tropical storm 40 NA NA #> 4 Erika 2003 tropical storm 45 NA NA #> 5 Erika 2003 tropical storm 50 NA NA #> 6 Erika 2003 tropical storm 55 NA NA #> 7 Erika 2003 tropical storm 60 NA NA #> 8 Erika 2003 hurricane 65 NA 30 #> 9 Erika 2003 hurricane 65 NA 30 #> 10 Erika 2003 tropical storm 35 NA NA #> 11 Erika 2003 tropical depression 25 NA NA# Can provide a secondary value for missing values when the conditions # are not met storms %>% mutate( ts_diameter = if_na(ts_diameter, then = wind, status == "tropical storm", otherwise = 0) )#> # A tibble: 11 x 6 #> name year status wind ts_diameter hu_diameter #> <chr> <dbl> <chr> <int> <dbl> <dbl> #> 1 Erika 2003 tropical storm 35 35 NA #> 2 Erika 2003 tropical storm 40 40 NA #> 3 Erika 2003 tropical storm 40 40 NA #> 4 Erika 2003 tropical storm 45 45 NA #> 5 Erika 2003 tropical storm 50 50 NA #> 6 Erika 2003 tropical storm 55 55 NA #> 7 Erika 2003 tropical storm 60 60 NA #> 8 Erika 2003 hurricane 65 0 NA #> 9 Erika 2003 hurricane 65 0 NA #> 10 Erika 2003 tropical storm 35 35 NA #> 11 Erika 2003 tropical depression 25 0 NA