How do I replace NA values with zeros in an R dataframe?

后端 未结 20 1947
说谎
说谎 2020-11-21 23:41

I have a data frame and some columns have NA values.

How do I replace these NA values with zeroes?

20条回答
  •  鱼传尺愫
    2020-11-22 00:08

    Dedicated functions, nafill and setnafill, for that purpose is in data.table. Whenever available, they distribute columns to be computed on multiple threads.

    library(data.table)
    
    ans_df <- nafill(df, fill=0)
    
    # or even faster, in-place
    setnafill(df, fill=0)
    

提交回复
热议问题