1

I need to remove rows in my data which satisfy more than one condition

the variables are

comorbidity
date_of_comorbidity 
date_of_birth

i want to remove the rows where both comorbidity is equal to 10 AND date of comorbidity is less than date of birth

I have tried

newdata<-subset(df, !(comorbidity1==10 & date_of_comorbidity<date_of_birth))

This seems to remove the observations when it is one or the other

I need it to be only when both of these criteria occur in the same row.

2 Answers 2

1

Try

newdata<-subset(df, comorbidity1 !=10 | date_of_comorbidity >= date_of_birth)
Sign up to request clarification or add additional context in comments.

3 Comments

This seems to be the same as what I have tried. I need it to check the date only when the comorbidity=10
This is strange, I have played around with a simulated dataset, and it worked out fine. If you try subset(df, !(comorbidity1 !=10 | date_of_comorbidity >= date_of_birth)), do you see instances with comorbidity == 10 and date of comorbidity less than date of birth?
The code above works for me (I can see when comorbidity == 10 and date of comorbidity less than date of birth) but when I try the first bit of code you gave me the number of observations drops by too much?
0

A similar solution with dplyr is:

library(dplyr)

newdata <- df %>% filter(comorbidity1 !=10 & date_of_comorbidity >= date_of_birth)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.