152

enter image description here

I wish to filter a data frame based on conditions in several columns. For example, how can I delete rows if column A = B and Column E = 0.

Henrik
  • 61,039
  • 13
  • 131
  • 152
AME
  • 5,176
  • 22
  • 68
  • 79
  • 2
    It is not a good idea to alter Q like this -- older answers get invalidated and it brings confusion. Try asking new question in the future. – mbq Nov 04 '11 at 09:05
  • related : http://stackoverflow.com/questions/6650510/remove-rows-from-data-frame-where-a-row-match-a-string – Joris Meys Nov 04 '11 at 12:18

3 Answers3

338

Logic index:

d<-d[!(d$A=="B" & d$E==0),]
mbq
  • 18,286
  • 6
  • 47
  • 72
99

Subset is your safest and easiest answer.

subset(dataframe, A==B & E!=0)

Real data example with mtcars

subset(mtcars, cyl==6 & am!=0)
Tyler Rinker
  • 103,777
  • 62
  • 306
  • 498
  • 1
    a good second example would be to filter on a list of values. ie subset of mtcars for cyl not in c(100, 200, 500) – airstrike Nov 09 '15 at 04:16
3

Use the which function:

A <- c('a','a','b','b','b')
B <- c(1,0,1,1,0)
d <- data.frame(A, B)

r <- with(d, which(B==0, arr.ind=TRUE))
newd <- d[-r, ]
Manuel Ramón
  • 2,470
  • 2
  • 17
  • 22
  • 5
    -1. Don't use `which` for this. Change the condition to `B==2` and see if it gives the answer that you want. See, e.g., http://rwiki.sciviews.org/doku.php?id=tips:surprises:traps#negative_array_indices – Richie Cotton Nov 04 '11 at 10:47
  • Why not to use `which` from [Advanced R](http://adv-r.had.co.nz/Subsetting.html): "there are two important differences. First, when the logical vector contains `NA`, logical subsetting replaces these values by NA while `which()` drops these values. Second, `x[-which(y)]` is not equivalent to `x[!y]`: if `y` is all `FALSE`, `which(y)` will be `integer(0)` and `-integer(0)` is still `integer(0)`, so you’ll get no values, instead of all values. In general, avoid switching from logical to integer subsetting unless you want, for example, the first or last TRUE value." – filups21 Sep 30 '19 at 14:17