How to impute missing values with row mean in R

前端 未结 2 457
孤独总比滥情好
孤独总比滥情好 2021-01-19 06:53

From a large data frame, I have extracted a row of numeric data and saved as a vector. Some of the values are missing and marked as NA. I want to impute the missing values w

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-19 07:20

    Use this:

    filter <- is.na(myVec)
    
    myVec[filter] <- colMeans(myDF[,filter], na.rm=TRUE)
    

    Where myVec is your vector and myDF is your data.frame.

提交回复
热议问题