0

I know how to select rows in a dataframe based on the values of two columns (using code from accepted answer on this question: Conditionally Remove Dataframe Rows with R)

My dataframe looks like this (simplified):

Date        Afd  Count
2012-03-23  12   0
2012-03-23  16   10
2012-03-23  17   12
2012-03-27  12   3
2012-03-27  16   9
2012-03-27  13   7
2012-03-27  22   5
2012-04-05  12   11
2012-04-05  23   8

Now I use this code:

df <- df[!(df$Afd=="12" & df$Date=="2012-03-23"),]

But I also want to drop another Afd and another Date. Following this logic: if Afd = 12 or 16, and Date = 2012-03-23 or 2012-03-27, then delete row.

Do I have to repeat this line 4 times with the different combinations or is there a better solution? (I suspect the latter)

Sotos
  • 51,121
  • 6
  • 32
  • 66
Tingolfin
  • 825
  • 8
  • 28
  • 2
    You mean this `df[!(df$Afd %in% c(12, 16) & df$Date %in% c("2012-03-23", "2012-03-27")),]`? – Sotos Jul 05 '17 at 09:55

1 Answers1

1

You can add or statements with | or use %in%

df <- subset(df, !(Afd %in% c("12", "16") & 
                   Date %in% as.Date(c("2012-03-23", "2012-03-27"))
                  )
            )
Gregor de Cillia
  • 7,397
  • 1
  • 26
  • 43
  • Yup. Exactly the same as my comment! – Sotos Jul 05 '17 at 11:55
  • I'm having problems with the date part though (this column is in actual date format). It doesn't react on it. If I run this code `df[!(df$Date %in% c("2013-03-11","2013-03-19")),]` nothing is removed. With this code `df[!(df$Date=="2013-03-11"),] df[!(df$Date=="2013-03-19"),]` the correct rows are deleted. What am I missing? – Tingolfin Jul 05 '17 at 12:27
  • 1
    @Tingolfin This is probabaly because of string to date conversion. Maybe `df$Date %in% as.Date(c("2013-03-11","2013-03-19")))` will work. If so, I will update my answer. @Sotos. Sorry, I actually updated my answer based on your comment to omit nested `& / |` calls. – Gregor de Cillia Jul 05 '17 at 17:32