Removing/Filtering rows based on condition in R

Viewed 605

I would like to remove participants who scored 2 in column EXP_MAN and 1 in Ethnicity_Rescuer.

I used the following code and it worked.

mydata <- mydata %>% 
  mutate(to_exclude = 
           case_when(EXP_MAN == 2 & 
                       (Ethnicity_Rescuer ==1) ~ 1)) %>%
  mutate(to_exclude = replace(to_exclude, is.na(to_exclude), 0))

mydata <- mydata %>% filter(to_exclude == 0)

However, this code seems very complicated and I am sure there should be a simpler solution.

I tried to filter out participants with the below code but it did not work. Just wondering what is the simplest code for removing participants in this case.

mydata <- mydata %>% filter(EXP_MAN != 2 & Ethnicity_Rescuer !=1)
1 Answers

You can use subset function to select a dataset and one/several conditions

subset(mydata,EXP_MAN != 2 & Ethnicity_Rescuer !=1)
Related