Select columns based on string match - dplyr::select
Within the dplyr world, try:
select(iris,contains("Sepal"))
See the Selection section in ?select
for numerous other helpers like starts_with
, ends_with
, etc.
You can try:
select(data, matches("search_string"))
It is more general than contains
- you can use regex (e.g. "one_string|or_the_other"
).
For more examples, see: http://rpackages.ianhowson.com/cran/dplyr/man/select.html.
No need to use select
just use [
instead
data[,grepl("search_string", colnames(data))]
Let's try with iris
dataset
>iris[,grepl("Sepal", colnames(iris))]
Sepal.Length Sepal.Width
1 5.1 3.5
2 4.9 3.0
3 4.7 3.2
4 4.6 3.1
5 5.0 3.6
6 5.4 3.9