why R return NA for all my functions?

I am trying to learn R but some errors stop me.
why R return NA for all my functions? I import my data and changed them to the data frame but even I want to calculate the mean it returns NA.

Here is my data information and my problem.

'data.frame': 30506 obs. of 84 variables: ...

Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
20.00 32.00 41.00 41.71 51.00 65.00 57

mean( df$SleepDuration[df$Gender == 1] )
[1] NA
[1] NA

1 Like

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:

# [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))


Yes, that's it.
thank you so much

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.