I'm aware of how to select variables from a large data.frame based on the column name containing one defined string, as in: (How do I select variables in an R dataframe whose names contain a particular string?)
But how do I do this to select columns from the object that contain either one string or another?
I'd prefer not to have to split and recombine the df, so that the columns would be kept in their original order.
Here is my sample code, using grep
, for obtaining variables matching the first string only, which works well:
df[grep("top",names(df),fixed=TRUE)]
grep won't take logical operators. So how do I select the second set of columns with "base" in the column name?
df[grep("top|base", names(df))]
? – talatstr_detect
from thestringr
package, like so:df[str_detect(names(df), "top|base")]
– grrgrrblafixed=TRUE
means that the operator won't work :) – jmk