0

Here is an example of a DF:

v1<-c(1,1,1,1,2,2,2,2,3,3,3,3)
v2<-c(234,457,234,675,235,205,347,578,695,783,200,697)
v3<-c(528,659,102,236,358,562,987,102,235,474,888,362)
df<- data.frame(v1,v2,v3)

For this example my intention is to remove the row with the v1=1, v2=457 and v3=659.

I'm trying different ways (subset or even []), for example:

subset(df,df$v1!=1 & df$v2!=457 & df$v3!=659)
df[df$v1!=1 & df$v2!=457 & df$v3!=659,]

However in both cases all rows with v1=1 are removed. How should I write this in order that just the row with v1=1, v2=457 and v3=659 is removed?

So the final DF should be like this:

v1<-c(1,1,1,2,2,2,2,3,3,3,3)
v2<-c(234,234,675,235,205,347,578,695,783,200,697)
v3<-c(528,102,236,358,562,987,102,235,474,888,362)
df<- data.frame(v1,v2,v3)
Cisco
  • 137
  • 7
  • Putting your row in another table and anti-joining is one way: `dplyr::anti_join(df, data.frame(v1 = 1, v2 = 457, v3 = 659))` https://stackoverflow.com/questions/28702960/find-complement-of-a-data-frame-anti-join Btw, you do not need `df$` inside `subset(df, ...)`. – Frank Apr 26 '18 at 18:42
  • Yeah, that does the same. I just need to remove the second row, the one with ALL that 3 conditions. – Cisco Apr 26 '18 at 18:49
  • `subset(df,df$v1!=1 | df$v2!=457 | df$v3!=659)` because **!(A & B)** is **!A | !B** BTW: in subset() you do not need `df$`, so `subset(df, v1!=1 | v2!=457 | v3!=659)` – jogo Apr 26 '18 at 18:53

2 Answers2

3

This seems to be correct logic

df[!(df$v1 == 1 & df$v2 == 457 & df$v3 == 659),]

What you are doing is removing all rows where v1!= 1 and v2!=457 and v3!=659

> v1<-c(1,1,1,1,2,2,2,2,3,3,3,3)
> v2<-c(234,457,234,675,235,205,347,578,695,783,200,697)
> v3<-c(528,659,102,236,358,562,987,102,235,474,888,362)
> df<- data.frame(v1,v2,v3)

> df
   v1  v2  v3
1   1 234 528
2   1 457 659
3   1 234 102
4   1 675 236
5   2 235 358
6   2 205 562
7   2 347 987
8   2 578 102
9   3 695 235
10  3 783 474
11  3 200 888
12  3 697 362

> df[!(df$v1 == 1 & df$v2 == 457 & df$v3 == 659),]
   v1  v2  v3
1   1 234 528
3   1 234 102
4   1 675 236
5   2 235 358
6   2 205 562
7   2 347 987
8   2 578 102
9   3 695 235
10  3 783 474
11  3 200 888
12  3 697 362
penguin
  • 1,267
  • 14
  • 27
  • ok but how I delete just the row that is v=1,v2=457,v3=659 but not the rest row with v1=1.... but the intersection of those 3 values... – Cisco Apr 26 '18 at 18:48
  • 2
    The mentioned code does exactly that, removes 2nd row where v1 = 1, v2 = 457 and v3 = 659 – penguin Apr 26 '18 at 18:50
  • @Cisco The result here has row numbers along the side 1, 3, 4, ... It seems to be correctly dropping only the second row. – Frank Apr 26 '18 at 18:51
  • I don't know why but in my R it removes all v1=1 – Cisco Apr 26 '18 at 18:58
  • 2
    OK. now I got it right... It was a problem with the packages I had loaded – Cisco Apr 26 '18 at 18:59
1

Close the conditions inside a bracket preceded by exclamation mark:

subset(df, !(df[,1] == 1 & df[,2] == 457 & df[,3] == 659))
rg255
  • 4,119
  • 3
  • 22
  • 40
  • I've run this on my pc and on www.rdrr.io/snippets and it works on both. Please paste exactly what you've run. – rg255 Apr 26 '18 at 19:03
  • OK. now I got it right... It was a problem with the packages I had loaded. I restart the session and worked perfectly. Thanks – Cisco Apr 26 '18 at 19:04
  • Great! Thanks for remembering to accept an answer too! – rg255 Apr 26 '18 at 19:07