if I have a data frame df
df=data.frame(x=1:20,y=c(1:10,rep(NA,10)),z=c(rep(NA,5),1:15))
I know to replace NAs with mean value for a given column is, we can use
df[is.na(df$x)]=mean(df$x,na.rm=T)
What I am trying to find is a way to use a single command so that it does this for the columns at once instead of repeating it for every column.
Suspecting, I need to use sapply and function, I tried something like this but clearly this does not work
sapply(df,function(x) df[is.na(df$x)]=mean(df$x,na.rm=T))
Any suggestions would be great. I tried to search previous post but I could not find a similar problem being addressed.