-1

I am trying to remove all rows where 4 and 5 are next to each other, so I just want to keep the lines that have a 4 and a 5 but with at least one number in between. Does anyone know how to do that?

structure(list(data_rel_1 = c("4 5 5 5", "4 5 NA NA", "4 5 5 5", 
"4 1 5 1", "4 5 5 5", "4 5 NA NA", "4 5 4 8", "4 10 5 5", "4 5 8 7"
)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", "data.frame"
))
Machavity
  • 30,841
  • 27
  • 92
  • 100
Theresa_S
  • 329
  • 1
  • 11

2 Answers2

0

Does this work:

library(dplyr)
library(stringr)
df %>% filter(str_detect(data_rel_1, '4\\s5', negate = TRUE))
# A tibble: 2 x 1
  data_rel_1
  <chr>     
1 4 1 5 1   
2 4 10 5 5  
Karthik S
  • 11,348
  • 2
  • 11
  • 25
0

In case looking only not to have 4 5 is ok, you can use grepl do subset the data.frame like:

D[!grepl("4 5", D[[1]]),]
#[1] "4 1 5 1"  "4 10 5 5"
GKi
  • 37,245
  • 2
  • 26
  • 48