subsetting or filtering out columns that contain letters in a string


I'm looking to create a data frame that excludes columns that contain a certain set of letters in the text.

I do not want to remove the text from the column, but remove the entire column from my new data frame.

P_only_lab_data_HI <- HI_Wide2022 %>% select(contains("MGG"))

This code selects all of the columns that do contain "MGG", but I want the opposite of that for a table that excludes column names containing "MGG"

How do I say something like "Does not contain"?

Thank-you in advance


Hi Sarah,

You're very very close! Negate your logical expression with ! and you're good to go:

dplyr::select(iris, dplyr::contains("Sepal")) |> names()
#> [1] "Sepal.Length" "Sepal.Width"

dplyr::select(iris, !dplyr::contains("Sepal")) |> names()
#> [1] "Petal.Length" "Petal.Width"  "Species"

Created on 2023-02-25 with reprex v2.0.2


Thanks so much for your reply. I'm so new to this that the way you wrote your code and the way I wrote mine are different and the language is still foreign. When I use your way, I end up with one column that contains all of my column names in a vertical fashion. But I want it to return the same Wide version but only return columns with out "MGG"

It tried the following and received error message.
P_only_lab_data_HI <- HI_Wide2022 %>% !select(contains("MGG"))
P_only_lab_data_HI <- HI_Wide2022 %>% select(!contains("MGG"))

It seems like there are so many different ways to get to the same idea it gets confusing to separate everything in my brain.

Thanks again!


Your second version should screen out columns whose name contains MGG. What error message did you get? Here is a simple example.

DF <- data.frame(A = 1:4, MGG = 2:5, A_MGG = 3:6, BFG = 4:7)
#> 1 1   2     3   4
#> 2 2   3     4   5
#> 3 3   4     5   6
#> 4 4   5     6   7
DFnew <- DF |> select(!contains("MGG"))
#>   A BFG
#> 1 1   4
#> 2 2   5
#> 3 3   6
#> 4 4   7

Created on 2023-03-02 with reprex v2.0.2

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.