69

I do I remove all rows in a dataframe where a certain row meets a string match criteria?

For example:

A,B,C
4,3,Foo
2,3,Bar
7,5,Zap

How would I return a dataframe that excludes all rows where C = Foo:

A,B,C
2,3,Bar
7,5,Zap
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
  • 1
    Related question: [Deleting specific rows from a data frame](http://stackoverflow.com/questions/6601658/deleting-specific-rows-from-a-data-frame). – Joshua Ulrich Jul 11 '11 at 13:13

6 Answers6

127

Just use the == with the negation symbol (!). If dtfm is the name of your data.frame:

dtfm[!dtfm$C == "Foo", ]

Or, to move the negation in the comparison:

dtfm[dtfm$C != "Foo", ]

Or, even shorter using subset():

subset(dtfm, C!="Foo")
Riad
  • 953
  • 3
  • 13
  • 23
Luciano Selzer
  • 9,806
  • 3
  • 42
  • 40
  • 4
    Or just `dftm[dtfm$C != "Foo", ]` which is the same but slightly more easier to read. – Sacha Epskamp Jul 11 '11 at 13:19
  • 6
    .. or `subset(dftm, C!="Foo")` – Karsten W. Jul 11 '11 at 14:01
  • 1
    How would you do this with an arbitrary number of conditions? Like if you wanted to remove all rows where "C = Foo" or "C = Bar"? – Zelbinian Mar 05 '13 at 15:11
  • 5
    That would be another question. But the key is to use %in% and !. In your example !(C %in% c("Foo", "Bar")) – Luciano Selzer Mar 05 '13 at 16:55
  • 1
    All good feedback. To complete Luciano's suggestion for the non-subset() example, I found that this worked to trim out undesired rows: dtfm <- dtfm[!(dtfm$C %in% c("Foo", "Bar")),] Just be sure not to forget the trailing comma in the [] brackets. – Robert Casey Feb 18 '14 at 21:11
12

You can use the dplyr package to easily remove those particular rows.

library(dplyr)
df <- filter(df, C != "Foo")
1

I had a column(A) in a data frame with 3 values in it (yes, no, unknown). I wanted to filter only those rows which had a value "yes" for which this is the code, hope this will help you guys as well --

df <- df [(!(df$A=="no") & !(df$A=="unknown")),]
Jason Roman
  • 8,146
  • 10
  • 35
  • 40
1

if you wish to using dplyr, for to remove row "Foo":

df %>%
 filter(!C=="Foo")
wesleysc352
  • 579
  • 1
  • 8
  • 21
0

I know this has been answered but here is another option:

library (dplyr)
df %>% filter(!c=="foo)
OR
df[!df$c=="foo", ]
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 02 '22 at 08:28
0

If your exclusion conditions are stored in another data frame you could use rows_delete:

library(dplyr)

removal_df <- data.frame(C = "Foo")

df %>% 
  rows_delete(removal_df, by = "C")

  A B   C
1 2 3 Bar
2 7 5 Zap

This is also handy if you have multiple exclusion conditions so you do not have to write out a long filter statement.

Note: rows_delete is only available if you have dplyr >= 1.0.0

LMc
  • 12,577
  • 3
  • 31
  • 43