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, apply
is idea:
apply(tr, 1, function(temp) {temp[is.na(temp)] <- mean(temp, na.rm=true); temp})
more information apply
-like functions here.
Comments
Post a Comment