It could be because you have NAs in your data. To ignore them, specify na.rm = TRUE in your mean()
Here is an example of data with NA:
hw <- c(5, NA, 0, 2)
This does not work:
mean(hw)
# [1] NA
But if you specify to remove NAs, it works:
mean(hw, na.rm = TRUE)
# [1] 2.333333
You can check if your data has NA like so: is.na(df$age) or any(is.na(df$age))