Back

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

To conditionally remove data frame rows, you can use the following code:

A <- c('A','B','A','B','A')> B <- c('C','D','C','D','C')> C <- c('E','F','E','F','E')> D <- c('G','H','G','H','G')> E <- c(1,0,1,0,1)> d <- data.frame(A, B, C, D, E)> d A B C D E1 A C E G 12 B D F H 03 A C E G 14 B D F H 05 A C E G 1

A <- c('A','B','A','B','A')

> B <- c('C','D','C','D','C')

> C <- c('E','F','E','F','E')

> D <- c('G','H','G','H','G')

> E <- c(1,0,1,0,1)

> d <- data.frame(A, B, C, D, E)

> d

A B C D E

1 A C E G 1

2 B D F H 0

3 A C E G 1

4 B D F H 0

5 A C E G 1

According to your condition:

d<-d[!(d$A=="B" & d$E==0),]> d A B C D E1 A C E G 13 A C E G 15 A C E G 1

d<-d[!(d$A=="B" & d$E==0),]

30.9k questions

32.9k answers

500 comments

665 users