# Conditionally Remove Dataframe Rows with R

Advertisement

## Conditionally Remove Dataframe Rows with R

### Question

Using R, how can I write the following logic into the dataframe: IF column A = B and Column E = 0, delete row

2019/06/26

### Popular Answer

Subset is your safest and easiest answer.

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

Real data example with mtcars

```
subset(mtcars, cyl==6 & am!=0)
```

2011/11/04

Read more... Read less...

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, ]
```

2011/11/04

Licensed under: CC-BY-SA with attribution

Not affiliated with: Stack Overflow

Email: [email protected]