1

This question is similar, but not duplicate of Delete rows containing specific strings in R and R, remove row if there is a certain character. The question is how to remove the row if a string is present anywhere in the row in a dataframe, without having to write the names of the columns in the function.
EDIT: in the answer remove ! if the objective is to keep those rows.

Ferroao
  • 3,042
  • 28
  • 53
  • The term generally used is *'filter'* as in *'filter rows where string present/not present...'* – smci Feb 01 '18 at 21:16

1 Answers1

2

You could do it this way.

Removing all rows which contain "bee"

df <- data.frame(c("a", "c", "d", "h"),c("bee","f","g","i"))

df<-df[apply(df,1,function(rowdata){
  !any(grepl("bee", rowdata))
  }),]

As @Ferroao pointed out above, in order to keep only rows which contain "bee" remove the ! (which stands for 'not' in this case).

If you wish to keep rows where every value contained "bee" you could use all() instead of any().

Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • 1
    Technically you would not like to do a forloop in a data, to be able to compute the row that has the bee then remove it. that would be tedious to the computer. you can something like `df[!grepl("bee",do.call(paste,df)),]` – Onyambu Feb 01 '18 at 20:54
  • That works, but I dont quite understand how the do.call and the paste are working together, you might consider posting a new answer, and I personally would enjoy an explanation of why it works :) – Rilcon42 Feb 01 '18 at 20:59
  • haha. you know how `do.call` works right? well then there will be little to explain.. Also this is an old question.. Just going through to see what I missed. – Onyambu Feb 01 '18 at 21:01