Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove rows containing string in any vector in data frame

Tags:

r

I have a data frame containing a number of vectors that contain strings I would like to remove rows that contain a certain string.

df <- data.frame(id=seq(1:10),
             foo=runif(10),
             sapply(letters[1:5],function(x) {sample(letters,10,T)} ),
             bar=runif(10))

This can be done on a single vector by specifying the vector name i.e.

df <- df[!grepl("b", df$a),]

which I can then repeat specifying each vector e.g.

df <- df[!grepl("b", df$b),]
df <- df[!grepl("b", df$c),]
df <- df[!grepl("b", df$d),]
df <- df[!grepl("b", df$e),]

but is it possible to do it in one line without having to specify which columns contain the string? Something like:

df <- df[!grepl("b", df),]
like image 331
dpel Avatar asked Oct 16 '25 14:10

dpel


1 Answers

You could try

df[-which(df=="b", arr.ind=TRUE)[,1],]

or, as suggested by @docendodiscimus

df[rowSums(df == "b") == 0,]

This second option is preferable because it does not lead to any difficulty if no matching pattern is found.

like image 56
RHertel Avatar answered Oct 18 '25 05:10

RHertel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!