1

This seems like a very simple problem but I am unable to find a solution anywhere online. I want to subset a data frame to rows for which any column contains the string "string". This is how I want to subset the data:

subset <- df[which(df$V1 == 'string' | df$V2 == 'string' | df$V3=='string'),]

But my frame has many columns and it is very inefficient to list all of them. Is there a more efficient way to select rows for which ANY column contains 'string'?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • 1
    Avoid names like `subset`. Not always important due to the presence of IDEs, but might spend too much time figuring out what went wrong if you ever need to use the function `subset`. Please also provide a sample of your data. – NelsonGon Jun 26 '19 at 15:41
  • Similar post, just with logic of *dropping* those rows instead of *keeping* them: https://stackoverflow.com/q/44544596/5325862 – camille Jun 26 '19 at 16:06

1 Answers1

3

We can use rowSums to create a create a logical vector for subsetting the rows

nm1 <- paste0("V", 1:3)
df[rowSums(df[nm1] == "string") >  0,]
akrun
  • 874,273
  • 37
  • 540
  • 662