Select columns based on string match - dplyr::select Select columns based on string match - dplyr::select r r

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.Width1          5.1         3.52          4.9         3.03          4.7         3.24          4.6         3.15          5.0         3.66          5.4         3.9