0

Dataframe:

A ; B
x   1
x   7
y   2
y   3
z   9
z   1

I want to remove all rows where A=x and A=z because x and z have value 1 in column B. Therefore, the dataframe must look like this:

A ; B
y   2
y   3

Thanks,

savioecon
  • 21
  • 3
  • 1
    See: [Remove multiple rows if condition for one row is met](https://stackoverflow.com/q/66531815/10488504) – GKi Mar 11 '21 at 12:38

3 Answers3

1

You can try subset

> subset(df, ! A %in% c("x","z"))
  A B
3 y 0
4 y 0

data

> dput(df)
structure(list(A = c("x", "x", "y", "y", "z", "z"), B = c(1L, 
0L, 0L, 0L, 0L, 1L)), class = "data.frame", row.names = c(NA,
-6L))
ThomasIsCoding
  • 53,240
  • 4
  • 13
  • 45
  • Thanks for your answer. My df has thousands of rows, so I want to remove the rows based on column B values. – savioecon Mar 11 '21 at 12:54
0
my.data.frame <- subset(data , A != "x" | A != "z")

Btw, duplicate of How to combine multiple conditions to subset a data-frame using "OR"?

vojtam
  • 373
  • 10
0

a data.table approach

library( data.table )
DT <- fread("A  B
x   1
x   7
y   2
y   3
z   9
z   1")


DT[ copy(DT)[, temp := sum(B == 1), by = A ]$temp == 0, ]

#    A B
# 1: y 2
# 2: y 3
Wimpel
  • 16,956
  • 1
  • 15
  • 34