trillian
trillian

Reputation: 49

R dataframe choosing columns by some part of the name

I have a large data frame with 341 variables. Some of them same type of data for different times. For example for viral load at initiation "t0vload", viral load at 6 moths "t6vload" and goes on. How can I write a command to subset all the columns with a name consisting "-vload"?

Upvotes: 1

Views: 73

Answers (2)

Vincent Bonhomme
Vincent Bonhomme

Reputation: 7433

For the record, using dplyr, the following should work as well:

 select(dat, ends_with("vload"))

or:

 dat %>% select(ends_with("vload"))

Upvotes: 1

akrun
akrun

Reputation: 886938

We can use grep

 dat[grep("vload$", names(dat))]

Upvotes: 2

Related Questions