Learn R Programming

tidyr (version 0.8.3)

replace_na: Replace missing values

Description

Replace missing values

Usage

replace_na(data, replace, ...)

Arguments

data

A data frame or vector.

replace

If data is a data frame, a named list giving the value to replace NA with for each column. If data is a vector, a single value used for replacement.

...

Additional arguments for methods. Currently unused.

Value

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.

See Also

na_if to replace specified values with a NA. coalesce to replace missing values with a specified value. recode to more generally replace values.

Examples

Run this code
# NOT RUN {
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"))
df %>% mutate(x = replace_na(x, 0))

# NULL are the list-col equivalent of NAs
df %>% replace_na(list(z = list(5)))

df$x %>% replace_na(0)
df$y %>% replace_na("unknown")
# }

Run the code above in your browser using DataLab