Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (5.3k points)

enter image description here

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

1 Answer

0 votes
by

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 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 E

1 A C E G 1

3 A C E G 1

5 A C E G 1

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...