I keep getting an error message when trying to use 'mean' function

Hi, I keep getting the following error message when I run this code and I do not know why. Please could someone suggest a solution to rectify this? Thanks in advance.

SF36_HT_M <- Full_data %>%
select(SF36HT_FOLLOW4) %>%
filter(!is.na(SF36HT_FOLLOW4)) %>%
mean() %>%
print()

Error: In mean.default(., !is.na(SF36HT_FOLLOW4)): argument is not numeric or logical: returning NA

The result of this code

Full_data %>%
select(SF36HT_FOLLOW4) %>%
filter(!is.na(SF36HT_FOLLOW4)) 

is a data frame. But the mean function expects a vector of values. It looks like you are trying to do something like the following. The code below returns a data frame with one row and one column:

SF36_HT_M <- Full_data %>%
  summarise(SF36HT_mean = mean(SF36HT_FOLLOW4, na.rm=TRUE))

Another possibility is to use the pull function to get a vector of values from a data column, which you can then run mean on directly. For example, the code below return a vector with one value (the mean of the "pulled" data):

SF36_HT_M <- Full_data %>%
  pull(SF36HT_FOLLOW4) %>%
  mean(., na.rm=TRUE)

Thank you very much!

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.