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.aggregate
fromzoo
. Loop through the columns of dataset (assuming all the columns arenumeric
), apply thena.aggregate
to replace the NA withmean
values (by default) and assign it back to the dataset.By default, the
FUN
argument ofna.aggregate
ismean
: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