0

Im trying to remove all rows that contain a ? in any column in a data frame. I have 950 rows by 11 columns. Ive tried this to do it all at once.

dataNew <- data %>% filter_all(all_vars(!grepl("?",.)))

and this to see if i could even get it to work for one column.

dataNew <- data[!grepl('?',data$column),]

Both of these attempts resulted in an empty dataframe. Any help is appreciated, thank you.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
dmccaul
  • 95
  • 8

1 Answers1

0

We can use fixed = TRUE as ? is a metacharacter (or escape (\\?) or wrap it inside square bracket ([?]) when the default mode for grep is fixed = FALSE

library(dplyr)
data %>%
    filter_all(all_vars(!grepl("?",., fixed = TRUE)))
#    col1 col2
#1    1    2

Or using across from the devel version of dplyr

data %>% 
    filter(across(everything(), ~ !grepl("?", ., fixed = TRUE)))
#  col1 col2
#1    1    2

Or using base R

data[!Reduce(`|`, lapply(data, grepl, pattern = '?', fixed = TRUE)),]

data

data <- data.frame(col1 = c("?", 1, 3, "?"), col2 = c(1, 2, "?", "?"),
      stringsAsFactors = FALSE)
akrun
  • 874,273
  • 37
  • 540
  • 662