Search code examples
rmultiple-columns

select columns based on first letter of columns using grep or grepl in r


I am attempting to use the dplyr package to select all columns that start with i. I have the following code:

dat <- select(newdat1,starts_with("i"))

and the colnames for my data are:

> colnames(newdat)
[1] "i22" "i21" "i20" "i24"

It is just a coincidence in this case they all start with i, as in other cases there will be a larger variety; thus, I want to automate the process. The issue is it appears my code using dplyr is correct; however, I am having issues with the package, so I was wondering if/how to accomplish the same task with grep or grepl, or anything really using the base package. Thanks!


Solution

  • With base R , you can use grep to match column names. You can use

    dat <- newdat1[, grep("^i", colnames(newdat1))]
    

    to do a starts-with like query. You can use any regular expression you want as the pattern in grep().