How to substitute NA by 0 in 20 columns?

前端 未结 4 1840
遥遥无期
遥遥无期 2021-01-02 10:25

I want to substitute NA by 0 in 20 columns. I found this approach for 2 columns, however I guess it\'s not optimal if the number of columns is 20. Is there any alternative a

4条回答
  •  礼貌的吻别
    2021-01-02 11:13

    Here is a tidyverse way to replace NA with different values based on the data type of the column.

    library(tidyverse)
    
    dataset %>% mutate_if(is.numeric, replace_na, 0) %>%  
        mutate_if(is.character, replace_na, "")
    

提交回复
热议问题