1

I want to delete all the rows which has the character *.

When I try to delete * - it just deletes everything

("*" - how to avoid it from selecting everything?)

xx <- data.frame(
  c1=c(".", ".", ".", ".", ".", "*", ".", ".", "."),
  c2=c(".", ".", ".", ".", "Q", "Q", "R", ".", "."),
  c3=c(".", ".", ".", ".", "W", "*", ".", ".", "."),
  c4=c("A", "A", ".", ".", "I", ".", "P", ".", "."),
  c5=c(".", ".", ".", ".", "D", "Q", "D", ".", "."),
  c6=c(".", ".", ".", ".", "*", ".", ".", ".", "."),
  c7=c(".", ".", ".", ".", "W", ".", ".", ".", "."),
  row.names = paste0("r", 1:9)
)
xx[!grepl('*', xx),]
dplyr::filter(xx, !grepl('*', c1))
xx %>% 
+     filter(!str_detect(xx, '*'))

The result should not have rows which has "*"

(delete row5 and row6 as they have *)

structure(list(c1 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("-", 
".", "*"), class = "factor"), c2 = structure(c(1L, 1L, 1L, 1L, 
4L, 1L, 1L), .Label = c(".", "A", "Q", "R", "T"), class = "factor"), 
    c3 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c(".", 
    "*", "S", "W"), class = "factor"), c4 = structure(c(2L, 2L, 
    1L, 1L, 4L, 1L, 1L), .Label = c(".", "A", "I", "P", "V"), class = "factor"), 
    c5 = structure(c(1L, 1L, 1L, 1L, 3L, 1L, 1L), .Label = c(".", 
    "A", "D", "K", "Q"), class = "factor"), c6 = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L), .Label = c("-", ".", "*"), class = "factor"), 
    c7 = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("-", 
    ".", "W"), class = "factor")), row.names = c("r1", "r2", 
"r3", "r4", "r7", "r8", "r9"), class = "data.frame")

Thanks!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
user11352627
  • 127
  • 8

1 Answers1

1

If we use grep, * is a metacharacter representing any zero or more character. We can either use fixed = TRUE or escape (\\*) to get the literal value

xx[!Reduce(`|`, lapply(xx, function(x) grepl("*", x, fixed = TRUE))),]

Or another option is == to match the *, get the count of matches in a row with rowSums, and subset

xx[!rowSums(xx == "*"),]
akrun
  • 874,273
  • 37
  • 540
  • 662