Skip to content
Prev 369935 / 398503 Next

selecting dataframe columns based on substring of col name(s)

Assume there 100 columns, named col1, col2,..., col100 in data frame d
+ maybe some more columns with various names preceding them. You want
col21 to col72.

nm <- names(d)
d[, which(nm == "col21"): which(nm == "col72") ]

## NB : if all you have is col1 to col100 the d[, 23:72] works fine.

See any good tutorial on R for how to index matrix like structures in R.

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Jun 21, 2017 at 9:11 AM, Evan Cooch <evan.cooch at gmail.com> wrote: