This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to select columns from a dataframe of which the name ends with a specific word

Hi guys,

I've to create a variable a in which elements are column names of a database.

For example, I have the following column names of a df:

colnames(df)
"PS_01", "PS_01_mod2", "PS_02", "PS_02_mod2"

I want to create a vector a in which elements are the column names of df ending with mod2, so I want to create this situation

a <- "PS_01_mod2", "PS_02_mod2"

How can I do this in a simple way?

r

3 answers

Hi, a simple grep() command will do this for you, and we can add a regular expression ('regex') to ensure positional specificity:

vector <- c('PS_01', 'PS_01_mod2', 'PS_02', 'PS_02_mod2', 'mod2_mod1')
idx <- grep('mod2$', vector)
vector[idx]
[1] "PS_01_mod2" "PS_02_mod2"

The dollar, $, means that we only want 'mod2' appearing at the end of a line.

Note the difference here, without the dollar:

idx <- grep('mod2', vector)
vector[idx]
[1] "PS_01_mod2" "PS_02_mod2" "mod2_mod1"

Kevin

Or return the value, instead of index:

grep("mod2$", vector, value = TRUE)
# [1] "PS_01_mod2" "PS_02_mod2"

Look into grep, it's a very useful function in R and UNIX command as well.

a <- grep("_mod2$", colnames(df), value=TRUE)

the dollar sign indicates this pattern should be at the end only.

Avoiding regex, using a dedicated function, endsWith:

vector[ endsWith(vector, "mod2") ]
# [1] "PS_01_mod2" "PS_02_mod2"

Log in to answer this question.