Skip to content
Prev 369932 / 398503 Next

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

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.