Home > Net >  removing rows with specific values
removing rows with specific values

Time:10-15

I would like to remove rows that have specific values in a given column

id     x_col
1       c4_2342
1       c4_2122 
2       c3_1212
2       c39990

I want to remove everything that has "c4"

id     x_col
2       c3_1212
2       c39990

Can someone please help?

CodePudding user response:

Variation on @akrun:

library(dplyr)
library(stringr)

df1 %>%
   filter(!str_detect(x_col, "c4"))

CodePudding user response:

For subsetting rows, use filter in dplyr with a logical expression created by checking for 'c4' substring in the 'x_col' with str_detect and use negate = TRUE

library(dplyr)
library(stringr)
df1 %>%
   filter(str_detect(x_col, "c4", negate = TRUE))

-output

id   x_col
1  2 c3_1212
2  2  c39990

Or in base R

subset(df1, !grepl("c4", x_col))

data

df1 <- structure(list(id = c(1L, 1L, 2L, 2L), x_col = c("c4_2342", "c4_2122", 
"c3_1212", "c39990")), class = "data.frame", row.names = c(NA, 
-4L))

CodePudding user response:

Another option using grep like this:

df <- read.table(text = 'id     x_col
1       c4_2342
1       c4_2122 
2       c3_1212
2       c39990', header = TRUE)

df[- grep("c4", df$x_col),]
#>   id   x_col
#> 3  2 c3_1212
#> 4  2  c39990

Created on 2022-10-14 with reprex v2.0.2

  • Related