Getting NA when calculating the mean from a dataset

Hello,

I have a dataset I've cleaned the data in. No Nas. However when I try to calculate the mean from a specific column called 'Age', i get a 'NA' result. Min and Max age as well as Sd are correctly calculated.
The code I'm using is:

age_desc <- dem_data2 %>%
summarise(mean = mean(Age), na.rm = T,
sd = sd(Age), na.rm = T,
min = min(Age), na.rm = T,
max = max(Age), na.rm = T)
age_desc

Any help would be greatly appreciated.

To add, it returns the message 'argument is not numeric or logical: returning NA'

Hi maarkar,

I think if you close your brackets after na.rm = T you should see the results you are expecting...

age_desc <- dem_data2 %>%
  summarise(
    mean = mean(Age, na.rm = T),
    sd = sd(Age, na.rm = T),
    min = min(Age, na.rm = T),
    max = max(Age, na.rm = T)
)

age_desc

Hope that works.

Hello,

Thank you for the fast response. I've tried doing that and the result is pretty much the same.. I think it could be that Age is coded as a character variable? But I am not sure how to change this since i get similar results with as.numeric(dem_data2$Age)?

Thank you.

Hi maarkar

Ah ok, you're right if Age is coded as a character variable then try converting it to an integer column...

dem_data2 <- 
  dem_data2 %>% 
  type_convert(
    col_types = cols(Age = col_integer())
  )

Then I think you should be able to calculate you summary stats. Hope this works for you now.

This topic was automatically closed 21 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.