I have a data frame and I would like to impute missing values based on row mean instead of column mean.
id Price1 Price2 Price3 Price4 HorizontalMean
004 NA 101 103 114 106
005 100 108 78 99 96.25
006 34 33 NA 78 48.333
...
I’ve looked at a few packages and can’t seem to find any that explicitly mention it. Any recommendations or do I need to do a transpose first (this could be a problem as the data is >100k lines).
Here is a nice little one-liner:
EDIT: for @Charlie who asked how would you do to replace NAs with column means, you could use the same thing but replace rowMeans(…) with a vector repeating the column means:
or apply ifelse to each column of the list: