Bindukumar Jampana Bindukumar Jampana - 9 months ago 35
R Question

How can I get the average (mean) of selected columns and impute the NA's

this question can be viewed as extension to the thread below:
(How can I get the average (mean) of selected columns). How do we impute the missing values ie., NA"s using the mean of the selected columns.


One option is na.aggregate from zoo to impute the missing values (NA) with the mean value of that column. We loop through the selected columns of dataset (lapply(df1[4:8], .), apply the function and then update the columns on the lhs of <-

df1[4:8] <- lapply(df1[4:8], na.aggregate)

If we need the median, use the FUN as median (by default it is mean)

df1[4:8] <- lapply(df1[4:8], na.aggregate, FUN = median)