How could I subset a dataframe by a string that could be found anywhere in its columns? For example
"ol" exists only in the third row so that would give us only that row as result.
col1<-c("sd fgg","df dfg","fgh gdfg")
col2<-c("sd fgg","df dgfg","fgh gdfg")
col3<-c("sd fggg","dfg dfgg","fgghol gdfg")
df<-data.frame(col1,col2,col3)
>Solution :
Using R base
> df[unlist(sapply(df, function (x) grep("ol", x))), ]
col1 col2 col3
3 fgh gdfg fgh gdfg fgghol gdfg
A dplyr approach
df %>%
slice(.,
rowwise(.) %>%
grep("ol", .) )
col1 col2 col3
1 fgh gdfg fgh gdfg fgghol gdfg