-6

I wish to filter a dataframe shown in table 1 so it looks like table 2 by removing any rows containing "Pathogenic" in the class column and 0 in the validated column. Although, I am unsure which tool I should use to accomplish this.

Table1

Class               Validated
Pathogenic             1
Pathogenic             1
Pathogenic             0
Pathogenic             0
Likely Pathogenic      1
Likely Pathogenic      0
Likely Pathogenic      1
Uncertain              0
Uncertain              1


Table2

Class               Validated
Pathogenic             1
Pathogenic             1
Likely Pathogenic      1
Likely Pathogenic      0
Likely Pathogenic      1
Uncertain              0
Uncertain              1
David Ross
  • 1,087
  • 1
  • 14
  • 21
  • 2
    Please don't post images. We can't copy from your image. Show a reproducible example with `dput`. Some guidelines to create the reproducible example is in [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – akrun Aug 26 '16 at 11:38
  • Possible duplicate of [How to combine multiple conditions to subset a data-frame using "OR"?](http://stackoverflow.com/questions/4935479/how-to-combine-multiple-conditions-to-subset-a-data-frame-using-or) – jogo Aug 26 '16 at 11:57
  • 2
    what exactly is your condition to move from table1 to table2 ? – Cath Aug 26 '16 at 12:13
  • 1
    I wish to remove any rows containing "Pathogenic" in the class column and 0 in the validated column. Apologies for the lack of clarity. – David Ross Aug 26 '16 at 12:52

2 Answers2

3

Assuming that the type of your "Validated" column is numeric:

table2 <- table1[!(table1$Class == "Pathogenic" & table1$Validated == 0),]
kaksat
  • 709
  • 1
  • 7
  • 18
0

One option based on the OP's clarification in the comments would be using data.table

library(data.table)
setDT(Table1)[!(Class == "Pathogenic" & Validated == 0) ]
#               Class Validated
#1:        Pathogenic         1
#2:        Pathogenic         1
#3: Likely Pathogenic         1
#4: Likely Pathogenic         0
#5: Likely Pathogenic         1
#6:         Uncertain         0
#7:         Uncertain         1

Or after setting the key

setDT(Table1, key = c("Class", "Validated"))[!.("Pathogenic", 0)]
#                  Class Validated
#1: Likely Pathogenic         0
#2: Likely Pathogenic         1
#3: Likely Pathogenic         1
#4:        Pathogenic         1
#5:        Pathogenic         1
#6:         Uncertain         0
#7:         Uncertain         1

EDIT: Previously, I was following a different logic as the OP's initial post was I wish to filter a dataframe shown in table 1 so it looks like table 2. Although, I am unsure which tool I should use to accomplish this.

data

df1 <- structure(list(Class = c("Pathogenic", "Pathogenic", "Pathogenic", 
 "Pathogenic", "Likely Pathogenic", "Likely Pathogenic", "Likely Pathogenic", 
"Uncertain", "Uncertain"), Validated = c(1, 1, 0, 0, 1, 0, 1, 
0, 1)), .Names = c("Class", "Validated"), row.names = c(NA, -9L
), class = "data.frame")
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662