How to substitute NA by 0 in 20 columns?

The replace_na function from tidyr can be applied over a vector as well as a dataframe (http://tidyr.tidyverse.org/reference/replace_na.html).

Use it with a mutate_at variation from dplyr to apply it to multiple columns at the same time:

my_data %>% mutate_at(vars(b,c,e,f), replace_na, 0)

or

my_data %>% mutate_at(c('b','c','e','f'), replace_na, 0)

Another option:

library(tidyr)
v <- c('b', 'c', 'e', 'f')
replace_na(df, as.list(setNames(rep(0, length(v)), v)))

Which gives:

#  a b c  d e f g d.1
#1 1 0 0  2 3 4 7   6
#2 2 g 3 NA 4 5 4   Y
#3 3 r 4  4 0 t 5   5

Tags:

R

Na