We can solve it in data.table
way with tidyr::repalce_na
function and lapply
library(data.table)
library(tidyr)
setDT(df)
df[,c("a","b","c"):=lapply(.SD,function(x) replace_na(x,0)),.SDcols=c("a","b","c")]
In this way, we can also solve paste columns with NA
string. First, we replace_na(x,"")
,then we can use stringr::str_c
to combine columns!