How to filter dataframe rows based on range of values

Hi, I am attempting to filter rows of my dataframe 'AGE' based on a range of values of the variable 'SF35PCS_HLEQ1', and then find the mean of the remaining 'AGE2' variable rows. However, I keep getting the same mean value output despite changing the range of values. How can I rectify this issue so that the range limitation command is effected?

AGE <- Full_data %>%
select(AGE2, SF36PCS_HLEQ1) %>%
filter(!is.na(AGE2), !is.na(SF36PCS_HLEQ1<=100), !is.na(SF36PCS_HLEQ1>=72)) %>%
summarise(AGE_mean = mean(AGE2)) %>%
print()

Thanks in advance.

The comparisons <= and >= return TRUE or FALSE, both of which will satisfy the !is.na() criterion. Try

AGE <- Full_data %>%
select(AGE2, SF36PCS_HLEQ1) %>%
filter(!is.na(AGE2), !is.na(SF36PCS_HLEQ1), SF36PCS_HLEQ1<=100, SF36PCS_HLEQ1>=72) %>%
summarise(AGE_mean = mean(AGE2)) %>%

Hi, thank you for your suggestion. This method seems to work for some variables, and not for others. For instance, if I try and run the following code, I keep getting the same output for the mean of 'AGE', despite changing the cutoff for the 'DEMENTIA_COHORTDATE1' variable, suggesting that the 'DEMENTIA_COHORTDATE1 >=2004' command is not being applied:

AGE <- Full_data %>%
select(AGE3, DEMENTIA_DEFINITE_ALL_2018, DEMENTIA_COHORTDATE1) %>%
filter(!is.na(AGE3), AGE3>=55, !is.na(DEMENTIA_DEFINITE_ALL_2018, !is.na(DEMENTIA_COHORTDATE1), DEMENTIA_COHORTDATE1>=2004)

Thanks again,
Renuka

this bracket is unclosed

Hi, sorry, I made a typo there, I had included the bracket in the code but the issue still remains.

Please post the output of

tmp<- Full_data %>%
select(AGE3, DEMENTIA_DEFINITE_ALL_2018, DEMENTIA_COHORTDATE1)

dput(head(tmp, 20))

Put a line with three back ticks just before and after the pasted output, like this
```
Your output here
```

check is.na only has the one argument. It looks like you call it with 3 but not sure that is what you meant

!is.na(DEMENTIA_DEFINITE_ALL_2018, !is.na(DEMENTIA_COHORTDATE1), DEMENTIA_COHORTDATE1>=2004)
1 2 3

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.