I'm trying to write a function to replace missing values in columns with median, and that this works for both factors/characters and numerical values.
library(dplyr)
test = data.frame(a=1:6,b=c("a","b",NA,NA,NA,"c"),c=c(1,1,1,1,2,NA),d=c("a","a","c",NA,NA,"b"))
fun_rep_na = function(df){
for(i in colnames(df)){
j<-sym(i)
df = df %>% mutate(!!j=if_else(is.na(!!j),median(!!j, na.rm=TRUE),!!j))
}
}
I see that tidyr has a function called replace_na, but I'm not sure how to use this either. Anyway, a custom function is what I would like.
The code above gives me an error.