Skip to content
Prev 369937 / 398503 Next

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

After staring at the code for the base function subset with a thought to hacking it to do this I realized that should be already part of the evaluation result from its current form:

 names(airquality)
#[1] "Ozone"   "Solar.R" "Wind"    "Temp"    "Month"   "Day"  

subset(airquality, 
          Temp > 90,             # this is the row selection
          select = Ozone:Solar.R) # and this selects columns
#--------
    Ozone Solar.R
42     NA     259
43     NA     250
69     97     267
70     97     272
75     NA     291
102    NA     222
120    76     203
121   118     225
122    84     237
123    85     188
124    96     167
125    78     197
126    73     183
127    91     189

Bert's advice to work with the numbers is good, but conversion to numeric designations of columns inside the `select`-expression is actually what is occurring inside `subset`.