Sebastian Zeki
Sebastian Zeki

Reputation: 6874

How to filter rows that contain a string in any column in a dataframe

Using the mtcars dataframe, how can I get a new dataframe that contains the string "3"

So far I have:

mtcars<-lapply(mtcars, function(x) as.character(x))
myindices<-sapply(mtcars, function(x) { grep("3",x, ignore.case = TRUE) })

This gives me a list of indices. How do I just get a filtered dataframe from the original.

Feel free to criticise my approach, it is the end result that I am really interested in

Upvotes: 2

Views: 1999

Answers (5)

jay.sf
jay.sf

Reputation: 72683

We may use toString.

mtcars.3 <- mtcars[grep("3", apply(mtcars, 1, toString)), ]

Check:

rbind(mtcars=dim(mtcars), mtcars.3=dim(mtcars.3))
         [,1] [,2]
mtcars     32   11
mtcars.3   31   11

Upvotes: 0

acylam
acylam

Reputation: 18661

We can use filter_all from dplyr. This returns a dataframe with rows that has at least one column containing the string "3":

library(dplyr)

mtcars %>%
  filter_all(any_vars(grepl("3", .)))

If we want a dataframe with rows that has all columns containing the string "3". We use all_vars instead of any_vars:

mtcars %>%
  filter_all(all_vars(grepl("3", .)))

Upvotes: 7

Andrew
Andrew

Reputation: 5138

Similar to your sapply solution:

mtcars[sapply(1:nrow(mtcars), function(i) any(grepl("3", mtcars[i,], fixed = T))),]

Or, you could do this as well:

mtcars[grepl("3", do.call(paste0, mtcars), fixed = T),]

Upvotes: 3

iod
iod

Reputation: 7592

Another base R solution:

mtcars[apply(mtcars,1,function(x) grepl("3",paste(x,collapse=""))),]

Upvotes: 0

akrun
akrun

Reputation: 887048

We can uses grepl with Reduce from base R

out <- mtcars[Reduce(`|`, lapply(mtcars, grepl, pattern = "3")),]
dim(out)
#[1] 31 11

Upvotes: 4

Related Questions