I have a df from a psychology inventory answers. There are some NA values that I want to change with the mean of the column values. I have tried two methods
the first is create a function that ill use later with a sapply:
nar <- function(x) {x[is.na(x)] <<- round(mean(x, na.rm = TRUE))}
but when I proved the function, it did not work. However this code actually works with a single column:
x[is.na(x)] <<- round(mean(x, na.rm = TRUE)
so I found that i could do also a ifelse, that also works with one column but i dont know how to apply it to all the columns (i tried sapply and apply and it didn't work). However i dont know what to put as NO argument in the ifelse function. I just want it to do nothing but i don't know how to do that.
ifelse(is.na(dfv[,col]) == TRUE, dfv[,col][is.na(dfv[,col])] <- round(mean(dfv[,col], na.rm = TRUE)), help)
So may question is: How can I replace NA values with the mean of the column values where that NA value is in a whole data frame automatically?
I'll appreciate any help