I'm trying to replace NA values in several columns by the mean value of all these columns. The mean value is suppose to be calculated by row.
I've tried this code but the NAs don't get replaced:
ID Price1 Price2 Price3 Price4
1 2.1 3 4 NA
2 2 3 4.5 NA
3 2 NA 4 NA
4 NA 3 4 NA
price_cols <- c("Price1", "Price2", "Price3", "Price4")
data %>%
mutate_at(price_cols, funs(if_else(is.na(.), mean(price_cols, na.rm = TRUE), as.double(.))))
I've also tried adding rowwise() to the piping chain but still nothing. I know it has to do with the code not really taking the mean across rows but I don't know how to change it so it does. Help!