如果我们正在使用
~
,然后指定
.
也即
df %>%
mutate_if(function(.x) inherits(.x, c("integer", "numeric")),
~ replace_na(., 0))
# A tibble: 6 x 3
# id x y
# <chr> <dbl> <chr>
#1 A 1 a
#2 A 2 <NA>
#3 A 0 c
#4 B 10 <NA>
#5 B 0 <NA>
#6 B 30 f
否则,就这么做吧
df %>%
mutate_if(function(.x) inherits(.x, c("integer", "numeric")),
replace_na, replace = 0)
# A tibble: 6 x 3
# id x y
# <chr> <dbl> <chr>
#1 A 1 a
#2 A 2 <NA>
#3 A 0 c
#4 B 10 <NA>
#5 B 0 <NA>
#6 B 30 f
或者另一个变化是
df %>%
mutate_if(funs(inherits(., c("integer", "numeric"))),
~ replace_na(., 0))