0

How do I select multiple columns by name without having to type out each name.

For example I have the following code:

CTDB[, c(
"ENJOY_TV_RADIO_CHILD", 
"ENJOY_FMLY_CLOSE_FRND_CHILD", 
"ENJOY_HOBBIES_CHILD", 
"ENJOY_FAV_MEAL_CHILD",
"ENJOY_SHOWER_CHILD", 
"ENJOY_SCENT_CHILD", 
"ENJOY_PPL_SMILE_CHILD", 
"ENJOY_LOOK_SMART_CHILD", 
"ENJOY_READ_CHILD", 
"ENJOY_FAV_DRINK_CHILD", 
"ENJOY_SMALL_THINGS_CHILD", 
"ENJOY_LANDSCAPE_CHILD", 
"ENJOY_HELP_OTHR_CHILD", 
"ENJOY_PRAISE_CHILD")] <-revalue(as.matrix(CTDB[, c(
"ENJOY_TV_RADIO_CHILD", 
"ENJOY_FMLY_CLOSE_FRND_CHILD",
"ENJOY_HOBBIES_CHILD", 
"ENJOY_FAV_MEAL_CHILD", 
"ENJOY_SHOWER_CHILD", 
"ENJOY_SCENT_CHILD", 
"ENJOY_PPL_SMILE_CHILD", 
"ENJOY_LOOK_SMART_CHILD", 
"ENJOY_READ_CHILD", '
"ENJOY_FAV_DRINK_CHILD", 
"ENJOY_SMALL_THINGS_CHILD", 
"ENJOY_LANDSCAPE_CHILD", 
"ENJOY_HELP_OTHR_CHILD", 
"ENJOY_PRAISE_CHILD")]), c("0"=3, "1"=2, "2"=1, "3"=0))

All the columns are in order but instead of selecting by number like below

CTDB[,74:87] <-revalue(as.matrix(CTDB[,74:87]), c("0"=3, "1"=2, "2"=1, "3"=0))

I would like to select by the name of the column.

Thank you!

5
  • What do you mean? Why would you not want the numbers? Commented Feb 9, 2018 at 18:49
  • Do the columns you want to select all start with ENJOY_ and end with CHILD? And if so, do you want to select all of them? Commented Feb 9, 2018 at 18:50
  • @Onyambu I don't want the numbers, because the position of the columns can change in the dataset. Commented Feb 9, 2018 at 18:53
  • @RuiBarradas Yes, all the columns start with ENJOY_ and end with CHILD and yes I want to select everything that starts with ENJOY_ and ends with CHILD Commented Feb 9, 2018 at 18:55
  • what criterion can you use to identify the columns you need. What do they all have in common? Commented Feb 9, 2018 at 18:55

2 Answers 2

3

You should use grep or grepl

CTBD[,grep("^ENJOY.*CHILD$",colnames(CTBD)]

or

CTBD[,grepl("^ENJOY.*CHILD$",colnames(CTBD)]
Sign up to request clarification or add additional context in comments.

5 Comments

I got this error message: Error in grep("ENJOY.*CHILD", x) : object 'x' not found
Thank you! I got this warning message: "Warning message: In matrix(value, n, p) : data length [49890] is not a sub-multiple or multiple of the number of columns [14]" Do you know what it means?
are you trying to create another matrix? you have 14 columns but the datelength that you have cannot be divided into 14 columns
If CTBD is a matrix then you have to use colnames, which also works with data frames.
I still get this message "Warning message: In matrix(value, n, p) : data length [49890] is not a sub-multiple or multiple of the number of columns [14]"
1

If you need to do this as part of a pipe, you can also use dplyr::select and its helper functions in two equivalent ways, including one that can avoid regular expressions:

CTBD %>% select(matches("^ENJOY.*CHILD$"))
CTBD %>% select(intersect(starts_with("ENJOY"), ends_with("CHILD")))

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.