Re: [R] selecting dataframe columns based on substring of col name(s)

2017-06-22 Thread Evan Cooch
Thanks to all the good suggestions/solutions to the original problem. On 6/21/2017 3:28 PM, David Winsemius wrote: >> On Jun 21, 2017, at 9:11 AM, Evan Cooch wrote: >> >> Suppose I have the following sort of dataframe, where each column name has a >> common structure: prefix, followed by a numbe

Re: [R] selecting dataframe columns based on substring of col name(s)

2017-06-21 Thread David Winsemius
> On Jun 21, 2017, at 9:11 AM, Evan Cooch wrote: > > Suppose I have the following sort of dataframe, where each column name has a > common structure: prefix, followed by a number (for this example, col1, col2, > col3 and col4): > > d = data.frame( col1=runif(10), col2=runif(10), col3=runif(10

Re: [R] selecting dataframe columns based on substring of col name(s)

2017-06-21 Thread Bert Gunter
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 an

Re: [R] selecting dataframe columns based on substring of col name(s)

2017-06-21 Thread Jeff Newmiller
d[ , paste( "col", 2:4 ) ] or d[ , sprintf( "col%d", 2:4 ) ] or d[ , grep( "^col[234]$", names( d ) ] Each approach has different ways of being flexible. -- Sent from my phone. Please excuse my brevity. On June 21, 2017 9:11:10 AM PDT, Evan Cooch wrote: >Suppose I have the following sort of

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

2017-06-21 Thread Evan Cooch
Suppose I have the following sort of dataframe, where each column name has a common structure: prefix, followed by a number (for this example, col1, col2, col3 and col4): d = data.frame( col1=runif(10), col2=runif(10), col3=runif(10),col4=runif(10)) What I haven't been able to suss out is h