简体   繁体   中英

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!

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM