0

I have a large dataframe which contain columns like this:

df <- data.frame(W0 = 1,
                 Response = 1,
                 HighResponse = 1,
                 Response.W0 = 1,
                 HighResponse.W0 =1) 

Now, in a for loop, I want to select a column based on whether they contain a specified string- Response, W0, HighResponse. My method of selecting the column is:

x <- dplyr::select(df, contains("HighResponse.W0"))  #this works
x <- dplyr::select(df, contains("HighResponse"))  #doesn't work. Selects HighResponse and HighResponse.W0
x <- dplyr::select(df, contains("Response")) #doesn't work. Selects Response, HighResponse, Response.W0, HighResponse.W0
x <- dplyr::select(df, contains("W0"))  #doesn't work. Selects W0, Response.W0, HighResponse.W0

How can I modify my column selection method, so that it only selects exact string? For ex, select only W0 or Response not the other matching strings.

3
  • You stated you are using these in a loop. Unless you are doing simulations where the current data points depend on the previous, you should learn to only use functions from the tidyverse package, eg-groupby, map, etc. Those will make your work neat and easily readable, and of course pipable Commented Nov 2, 2022 at 16:45
  • yes, I am doing further analysis on the selected columns. Commented Nov 2, 2022 at 17:05
  • seems you are doing the same analysis over and over. You can use map Commented Nov 2, 2022 at 17:05

1 Answer 1

3

Use anchors with matches to specify the beginning (^) and end ($) of the string:

dplyr::select(df, matches("^HighResponse$"))

Or, without contains:

dplyr::select(df, "HighResponse")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.