I have over 1500 columns in my dataset and 100+ of them contains at least one NA. I know I can replace NAs in a single column by
d$var[is.na(d$var)] <- mean(d$var, na.rm=TRUE)
but how do I do this too ALL the NAs in my dataset?
Thank you!
I have over 1500 columns in my dataset and 100+ of them contains at least one NA. I know I can replace NAs in a single column by
d$var[is.na(d$var)] <- mean(d$var, na.rm=TRUE)
but how do I do this too ALL the NAs in my dataset?
Thank you!
We can use
na.aggregatefromzoo. Loop through the columns of dataset (assuming all the columns arenumeric), apply thena.aggregateto replace the NA withmeanvalues (by default) and assign it back to the dataset.By default, the
FUNargument ofna.aggregateismean:To do this nondestructively:
or in one line:
If there are non-numeric columns, do this only for the numeric columns by creating a logical index first