-1

I am trying to delete entire rows that contain certain infromation for example:

I would like to get rid of every row that contians the string red.

color num day
red 10 Mon
blue 6 Tus
green 2 Thur
red 1 Wed
orange 7 Sun
pink 12 Fri
yellow 25 Sat
red 35 Fri
Cole
  • 23
  • 4
  • 1
    Hello Cole! as you might have seen in the tour or at https://stackoverflow.com/help/how-to-ask in the help centre. Researching if there are similar questions is also a practice at stackoverflow. Try searching for "condition drop rows". – Omniswitcher Aug 26 '22 at 21:23
  • Does this answer your question? [Delete rows containing specific strings in R](https://stackoverflow.com/questions/22249702/delete-rows-containing-specific-strings-in-r) – Andrea M Aug 26 '22 at 22:20
  • 1
    Does this answer your question? [Filter rows which contain a certain string](https://stackoverflow.com/questions/22850026/filter-rows-which-contain-a-certain-string) – AndrewGB Aug 26 '22 at 22:39

3 Answers3

1

You can use filter option to get rid of the rows where column has certain values.

library(dplyr)
df <- data.frame(color = c("red", "blue", "green", "red", "orange","pink","yellow","red"), 
                 num = c(10, 6, 2, 1, 7,12,25,35),
                 day=c("Mon","Tus","Thur","Wed","Sun","Fri","Sat","Fri"))
df %>%
  filter(color!='red')

enter image description here

Unkown
  • 67
  • 3
0

Using base R:

cleaned_df <- orig_df[orig_df$color != "red",]

# Or more safely
cleaned_df <- orig_df[! orig_df$color %in% "red",]

I say more safely because the first will give you trouble if the color column contains missing values.

Andrea M
  • 2,314
  • 1
  • 9
  • 27
farnsy
  • 2,282
  • 19
  • 22
0
  • We can use
library(dplyr)

df |> filter(!grepl("red" , color))
  • Output
   color num  day
1   blue   6  Tus
2  green   2 Thur
3 orange   7  Sun
4   pink  12  Fri
5 yellow  25  Sat
Mohamed Desouky
  • 4,340
  • 2
  • 4
  • 19