New to R and trying to do a "simple imputation exercise using the "presidents" dataset out of RStudio. I wish to substitute "NA" for the mean value. I've tried so many combinations but am lacking the understanding of what exactly is wrong with the statement below. "The replacement has zero rows" is telling me something but I'm not sure how to fix it. Any suggestions and advise would be appreciated. Thank you!
df_pres <- data.frame(presidents)
df_pres$y[is.na(df_pres$y)] = mean(df_pres$y, na.rm=TRUE)
Error in
$<-.data.frame
(*tmp*
, y, value = numeric(0)) : replacement has 0 rows, data has 120 In addition: Warning message: In mean.default(df_pres$y, na.rm = TRUE) : argument is not numeric or logical: returning NA