Replace missing values
replace_na(data, replace, ...)
data | A data frame or vector. |
---|---|
replace | If |
... | Additional arguments for methods. Currently unused. |
If data
is a data frame, returns a data frame. If data
is a
vector, returns a vector of class determined by the union of data
and
replace
.
na_if
to replace specified values with a NA
.
coalesce
to replace missing values with a specified
value.
recode
to more generally replace values.
library(dplyr) df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"), z = list(1:5, NULL, 10:20)) df %>% replace_na(list(x = 0, y = "unknown"))#> # A tibble: 3 x 3 #> x y z #> <dbl> <chr> <list> #> 1 1 a <int [5]> #> 2 2 unknown <NULL> #> 3 0 b <int [11]>#> # A tibble: 3 x 3 #> x y z #> <dbl> <chr> <list> #> 1 1 a <int [5]> #> 2 2 <NA> <NULL> #> 3 0 b <int [11]>#> # A tibble: 3 x 3 #> x y z #> <dbl> <chr> <list> #> 1 1 a <int [5]> #> 2 2 <NA> <dbl [1]> #> 3 NA b <int [11]>df$x %>% replace_na(0)#> [1] 1 2 0df$y %>% replace_na("unknown")#> [1] "a" "unknown" "b"