Answers for "dplyr replace na with 0"

R
2

dplyr replace na

df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"))

df %>% dplyr::mutate(x = replace_na(x, 0))
#> # A tibble: 3 x 2
#>       x y    
#>   <dbl> <chr>
#> 1     1 a    
#> 2     2 NA   
#> 3     0 b

# To replace NA in two columns
df %>% replace_na(list(x = 0, y = "unknown"))
#> # A tibble: 3 x 2
#>       x y      
#>   <dbl> <chr>  
#> 1     1 a      
#> 2     2 unknown
#> 3     0 b
Posted by: Guest on September-09-2021

Browse Popular Code Answers by Language