Annie Wright
Annie Wright

Reputation: 87

Removing rows from a data set with respect to multiple conditions in R

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.

Upvotes: 1

Views: 118

Answers (2)

Escurisse
Escurisse

Reputation: 15

A similar solution with dplyr is:

library(dplyr)

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

Upvotes: 0

EugenR
EugenR

Reputation: 176

Try

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

Upvotes: 1

Related Questions