Find and replace missing values with row mean

后端 未结 5 724
夕颜
夕颜 2020-12-16 18:26

I have a data frame with NAs and I want to replace the NAs with row means

c1 = c(1,2,3,NA)
c2 = c(3,1,NA,3)
c3 = c(2,1,3,1)

df = data.frame(c1,c2,c3)

>          


        
5条回答
  •  眼角桃花
    2020-12-16 19:28

    Another option is na.aggregate from library(zoo) after transposing the dataset

    library(zoo)
    df[] <- t(na.aggregate(t(df)))
    df
    #  c1 c2 c3
    #1  1  3  2
    #2  2  1  1
    #3  3  3  3
    #4  2  3  1
    

提交回复
热议问题