Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Rows From Data Frame where a Row matches a String

Tags:

dataframe

r

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 
like image 243
Kyle Brandt Avatar asked Jul 11 '11 13:07

Kyle Brandt


People also ask

How do I delete rows in Pandas Dataframe based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do I remove a row based on a cell value in R?

If we prefer to work with the Tidyverse package, we can use the filter() function to remove (or select) rows based on values in a column (conditionally, that is, and the same as using subset). Furthermore, we can also use the function slice() from dplyr to remove rows based on the index.


1 Answers

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") 
like image 183
Luciano Selzer Avatar answered Sep 18 '22 15:09

Luciano Selzer