用 mutate_if 和 replace_na 替换数字列上的 NA [英] Replace NA on numeric columns with mutate_if and replace_na
本文介绍了用 mutate_if 和 replace_na 替换数字列上的 NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果可能,我想使用 mutate_if
和 replace_na
的一些变体替换数字列中的 NA,但无法弄清楚语法.
I would like to replace NAs in numeric columns using some variation of mutate_if
and replace_na
if possible, but can't figure out the syntax.
df <-tibble(
first = c("a", NA, "b"),
second = c(NA, 2, NA),
third = c(10, NA, NA)
)
#> # A tibble: 3 x 3
#> first second third
#> <chr> <dbl> <dbl>
#> 1 a NA 10.0
#> 2 <NA> 2.00 NA
#> 3 b NA NA
最终结果应该是:
#> # A tibble: 3 x 3
#> first second third
#> <chr> <dbl> <dbl>
#> 1 a 0 10.0
#> 2 <NA> 2.00 0
#> 3 b 0 0
我的尝试如下:
df %>% mutate_if(is.numeric , replace_na(., 0) )
#>Error: is_list(replace) is not TRUE
推荐答案
df %>% mutate_if(is.numeric , replace_na, replace = 0)
# A tibble: 3 x 3
# first second third
# <chr> <dbl> <dbl>
#1 a 0 10.0
#2 NA 2.00 0
#3 b 0 0
这篇关于用 mutate_if 和 replace_na 替换数字列上的 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文