Loop for cleaning NAs in R -


is there way write code more effectively?

for (k in 1:ncol(tr)){   temp=tr[[k]]   temp[is.na(temp)] = mean(temp[!is.na(temp)])   tr[[k]]=temp } 

to replace loop matrices or dataframes, applyis idea:

apply(tr, 1,  function(temp) {temp[is.na(temp)] <- mean(temp, na.rm=true); temp}) 

more information apply-like functions here.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -