Home > Net >  Deleting rows with certain strings
Deleting rows with certain strings

Time:08-27

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

CodePudding user response:

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

CodePudding user response:

  • 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

CodePudding user response:

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.

  • Related