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.
Asked
Active
Viewed 6.1e+01k times
153
-
2It 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 Answers
340
Logic index:
d<-d[!(d$A=="B" & d$E==0),]

mbq
- 18,510
- 6
- 49
- 72
-
4I tried so many complicated answers -- none worked. Your solution is simple and brilliant. – WGray Aug 06 '15 at 20:49
-
5Actually, a simpler way of viewing it is: `foo.isolated <- subset(foo, !(sid == "sid104" & game.num == 7))` – WGray Aug 06 '15 at 21:01
-
1
-
NA's killed my two hours :D Note that it will also select NA's – Ioane Sharvadze Feb 24 '17 at 20:18
100
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
- 108,132
- 65
- 322
- 519
-
1a 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,490
- 2
- 18
- 23
-
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