-2

Looking for a way of purging rows from a data frame whose values do not match two sets of vectors, one set applies to column1, and the second to column2. I want this to be an either or both (i.e. if row x has a col1 match but not col2, row x is not purged). This is what I was going for but the code doesn't work:

purged_frame <- original_frame[(original_frame$Column1 %in% vectorsetforcol1 | original_frame$Column2 %in% vectorsetforcol2),]

Thanks!

Edit: need an either or both scenario - wasn't very clear in my original question - anybody?

amunategui
  • 1,130
  • 2
  • 11
  • 15
  • 1
    possible duplicate of [Subset a data frame based on value pairs stored in independent ordered vectors](http://stackoverflow.com/questions/22492884/subset-a-data-frame-based-on-value-pairs-stored-in-independent-ordered-vectors) – sriramn Apr 29 '14 at 23:26
  • Made an edit in my above question - I need an either or both situation. – amunategui Apr 30 '14 at 02:35
  • Not a duplicate (or link not obvious to the solution I am looking for), so I am still looking for a solution to this - thanks to those willing to help. – amunategui Apr 30 '14 at 04:36

1 Answers1

1

It is best to share a reproducible example, (How to make a great R reproducible example?)

Using the dataset available in base R mtcars and data(mtcars), create two conditional vectors

vectorsetforcol1<- mtcars$mpg[mtcars$mpg<15]
vectorsetforcol2<-unique(mtcars$carb[mtcars$carb==2])

Output condition 1: (mpg < 15)

> mtcars[mtcars$mpg %in% vectorsetforcol1,]
                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
Duster 360          14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
Camaro Z28          13.3   8  350 245 3.73 3.840 15.41  0  0    3    4

Output condition 2: (carb == 2)

> mtcars[mtcars$carb %in% vectorsetforcol2,]
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Honda Civic       30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Dodge Challenger  15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin       15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Pontiac Firebird  19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Porsche 914-2     26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa      30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Volvo 142E        21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> 

Combine conditions 1 and 2

> cond.df<-mtcars[(mtcars$mpg %in% vectorsetforcol1 | mtcars$carb %in% vectorsetforcol2  ),]
> cond.df
                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> 

Test condition 1: (NOT(mpg < 15))

The cases where condition 1 is violated are present because they follow condition 2 (carb ==2)

> cond.test.col1<-cond.df[!cond.df$mpg %in% vectorsetforcol1, ]
> cond.test.col1
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Hornet Sportabout 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Merc 240D         24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230          22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Honda Civic       30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Dodge Challenger  15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin       15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Pontiac Firebird  19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Porsche 914-2     26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa      30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Volvo 142E        21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
> 

Test condition 2: (NOT(carb == 2))

The cases where condition 2 is violated are present because they follow condition 1 (mpg <15)

> cond.test.col2<-cond.df[!cond.df$carb %in% vectorsetforcol2, ]
> cond.test.col2
                     mpg cyl disp  hp drat    wt  qsec vs am gear carb
Duster 360          14.3   8  360 245 3.21 3.570 15.84  0  0    3    4
Cadillac Fleetwood  10.4   8  472 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8  460 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8  440 230 3.23 5.345 17.42  0  0    3    4
Camaro Z28          13.3   8  350 245 3.73 3.840 15.41  0  0    3    4

This is the same approach as yours, if you had provided a working example someone would have pointed out the issue...

Community
  • 1
  • 1
Silence Dogood
  • 3,587
  • 1
  • 13
  • 17
  • This is exactly what I was looking for. I thought there was a syntax issue with my original code, obviously a logic one instead - thanks for pointing it out (and point taken on reproducibility). – amunategui Apr 30 '14 at 14:44