How to select values that have specific characters

I'm trying to filter a column to have rows with specific characters. I have tried to use the contains() but will get an error message that I need to use a selecting function. Then I nest the contains function in select(). I also tried to use str_detect, but also get an error message. I just want to select the rows that contain a specific set of letters.

Here is an example with a built-in dataset


iris %>% 
    select(contains("Len")) %>% 
#>   Sepal.Length Petal.Length
#> 1          5.1          1.4
#> 2          4.9          1.4
#> 3          4.7          1.3
#> 4          4.6          1.5
#> 5          5.0          1.4
#> 6          5.4          1.7

Created on 2020-06-05 by the reprex package (v0.3.0)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

How do you I indicate which column I want to filter out?

Sorry but I don't understand what you mean, as I said if you need help with a specific problem, please provide a reprex.

I'm trying to filter out a column where all the rows have a value that have specific characters.

df1 <- df %>% filter('Work Type' == select(contains("SEW"))

I'm trying to create a column, where all the values have the letters SEW in it

That is not valid syntax, to give you a solution please provide a reproducible example, your example is not reproducible since you are not providing sample data.

Let's say this is my dataframe


I want to select all the values that contain the letter a and filter out the rest

You are making this harder than it should be, Have you even read the reprex guide I gave you? I'm going to give you a solution using a proper reprex if you have further questions, I expect you to do the same.


# Sample data on a copy/paste friendly format
sample_df <- data.frame(
  stringsAsFactors = FALSE,
               col = c("aaa","abc","njl","weg",

# Coding solution
sample_df %>% 
    filter(str_detect(col, "a"))
#>   col
#> 1 aaa
#> 2 abc
#> 3 aaa
#> 4 abc

Created on 2020-06-05 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.