memy - 1 year ago 147

R Question

I have a data frame

`mydata <- data.frame(`

x1= as.factor(1:3),

x2= as.factor (4:6),

x3= as.factor(7:9),

x4= as.factor (2:7),

x5= as.factor(1:6),

x6= seq(0,600,len= 600),

x7= seq(0,1,len=600)

)

and I want to remove some rows in this with particular conditions. I did in this way:

`mydata1 <- mydata%>%`

filter(x1==1, x2==4, x3==7, x4==2, x5==1)%>%

anti_join(mydata,., by=c("x1", "x2", "x3", "x4","x5","x6" "x7"))

mydata2 <- mydata1%>%

filter(x1==3, x2==6 x3==9, x4==7, x5==6)%>%

anti_join(mydata1,., by=c("x1", "x2", "x3", "x4","x5","x6", "x7"))

There are a lot of rows that I want to remove, is there another way to do this?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You can combine logical tests with `&`

for *and* and `|`

for *or*. If you want to remove rows that meet all of the following

```
x1==1, x2==4, x3==7, x4==2, x5==1
```

Then do it like this:

```
filter(mydata, !(x1 == 1 & x2 == 4 & x3 == 7 & x4 == 2 & x5 == 1))
```

The row order is different, but the results are the same:

```
md1 = filter(mydata, !(x1 == 1 & x2 == 4 & x3 == 7 & x4 == 2 & x5 == 1))
identical(arrange(md1, x6), arrange(mydata1, x6))
# [1] TRUE
```

You can combine your conditions into one `filter`

this way:

```
md2 = filter(
mydata,
!(x1 == 1 & x2 == 4 & x3 == 7 & x4 == 2 & x5 == 1),
!(x1 == 3 & x2 == 6 & x3 == 9 & x4 == 7 & x5 == 6)
)
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**