The NaN result will happen if one of your BMI values is NaN. Try running this code
NewDF <- df%>%
select(BMI, Gender)%>%
filter(Gender== "Male" | Gender == "Female")
and then run summary(NewDF). Does the summary of the BMI column say it has any NA's? If so, add na.rm = TRUE to your calculations of the mean.
mean(BMI, na.rm = TRUE)