Remove row if any column contains a specific string

You can read the data using na.strings = 'no_data' to set them as NA and then simply omit NAs (or take complete.cases), i.e. (Using @akrun's data set)

d1 <- read.table(text = 'time   speed  wheels
 1    1:00      30 no_data
            2    2:00 no_data      18
            3 no_data no_data no_data
            4    3:00      50      18', na.strings = 'no_data', h=TRUE)

d1[complete.cases(d1),]
#  time speed wheels
#4 3:00    50     18

#OR

na.omit(d1)
#  time speed wheels
#4 3:00    50     18

We can use rowSums to create a logical vector and subset based on it

df1[rowSums(df1 == "no_data")==0, , drop = FALSE]
#   time speed wheels
#4 3:00    50     18

data

df1 <- structure(list(time = c("1:00", "2:00", "no_data", "3:00"), speed = c("30", 
"no_data", "no_data", "50"), wheels = c("no_data", "18", "no_data", 
"18")), .Names = c("time", "speed", "wheels"), class = "data.frame", 
row.names = c(NA, -4L))

akrun answer is quick, correct and simply as much is it can :) however if you like to make your life more complex you can also do:

dat
     time   speed  wheels
1    1:00      30 no_data
2    2:00 no_data      18
3 no_data no_data no_data
4    3:00      50      18

dat$new <- apply(dat[,1:3], 1, function(x) any(x %in% c("no_data")))
dat <- dat[!(dat$new==TRUE),]
dat$new <- NULL

dat
  time speed wheels
4 3:00    50     18

Two dplyr options: (using Akrun's data from this answer)

library(dplyr)

## using the newer across()

df1 %>% filter(across(everything(), ~ !grepl("no_data", .)))
#>   time speed wheels
#> 1 3:00    50     18

## with the superseded filter_all

df1 %>% filter_all(all_vars(!grepl("no_data", .)))
#>   time speed wheels
#> 1 3:00    50     18

Caveat:
This only works if you want to remove all rows with that string. If you want to get all rows with this string, all_vars(grepl('no_data',.) (without !) would not be sufficient: This would only get the rows where all columns contain the string. In this case, use filter_all(any_vars()) instead.

Tags:

R