Assuming your data as simple as this (but with more columns)...
df <- data.frame(a = c(0,1,NA), b = c(1,NA,2), c = c(NA,2,1))
...you can run mutate_all
from the dplyr
-package to apply Hmisc's mean imputation to all columns:
library(dplyr)
df %>% mutate_all(.funs = ~Hmisc::impute(.,mean))
a b c
1 0.0 1.0 1.5
2 1.0 1.5 2.0
3 0.5 2.0 1.0
If there are columns you don't want to or cannot impute (e.g. character columns), you'd have to slightly adjust the code and probably switch to mutate_at
, e.g.
df %>%
mutate_at(.vars = vars(a:c),
.funs = ~Hmisc::impute(.,mean))