I keep getting the error "x missing values and NaN not allowed if na.rm is false"

Hi, I am trying to filter data based on the 80th percentile, but I keep getting the following error:

AGE_M <- Full_data %>%
select(AGE2, SEX, SF36PCS_HLEQ1) %>%
filter(SEX == 1, !is.na(AGE2), !is.na(quantile(SF36PCS_HLEQ1, probs = c(0.8)))) %>%
summarise(AGE_mean_M = mean(AGE2, na.rm = TRUE)) %>%

Error: problem with filter() input 3. x missing values and NaNs not allowed if na.rm is FALSE.

Any suggestions would be much appreciated,

Add na.rm=TRUE as an additional parameter to quantile()

Hi, thanks for your suggestion. Indeed, this stops the error form occurring, but for some reason the 'probs = c(0.8)' input does not seem to be applied, as the same output is produced for the summarise command, despite changing the c() value. Is there any way of rectifying this? Thanks again!

What is your intent for that quantile to do ?
You have it set up so if its result is not NA that rows will be kept, I guess it's a low bar.

Hi, I would like to keep rows that fall within the 80th percentile of the SF36PCS_HLEQ1 variable whilst discarding NA values so that the mean function will work. Thanks

I don't have your data, but using the iris dataset, the equivalent approach might look something like :


# make an example dataset with some pesky NA values sprinkled in 
spoiliris <- iris
spoiliris[1,1] <- NA
spoiliris[10,2] <- NA
spoiliris[101,3] <- NA

in step by step

# determine what the 80 percentile of sepal.length variable is 
(q80 <- quantile(spoiliris$Sepal.Length,probs = 0.8,na.rm=TRUE))

#filter on the value
(spiris_within_q80 <- filter(spoiliris,
       Sepal.Length <= q80))

#summarise the result
          Petal.Width_mean = mean(Petal.Width,na.rm=TRUE))

in a more condensed form

       Sepal.Length <= quantile(Sepal.Length,probs = 0.8,na.rm=TRUE)) %>% 
summarise(  Petal.Width_mean = 

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.