It is straightforward to use dplyr to select columns using various helper functions, such as contains()
. In the help file for these functions the argument is referred to as a 'literal string'. However, is it possible to use regular expressions instead?
The following example works:
library(dplyr)
iris %>%
select(contains("Species"))
The following regex example does not:
# Select all column names that end with lower case "s"
iris %>%
select(contains("s$"))
# Not run
data frame with 0 columns and 150 rows
I would like to know if using regular expressions in dplyr select helper functions is possible and, if so, their implementation.
If this isn't possible, I will except an answer using an alternative method (e.g., base or data.table). For background, my ultimate aim is to use a summarise_at()
function or equivalent to sum all columns that end in a number (i.e, regexp [0-9]$
).
To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.
A regular expression lets you perform pattern matching on strings of characters. The regular expression syntax allows you to precisely define the pattern used to match strings, giving you much greater control than wildcard matching used in the LIKE predicate.
The select helper function matches()
is available to match regular expressions:
library(dplyr)
out <- select(iris, matches("s$"))
head(out)
#> Species
#> 1 setosa
#> 2 setosa
#> 3 setosa
#> 4 setosa
#> 5 setosa
#> 6 setosa
With dplyr
, one can use ends_with
:
iris %>%
select(ends_with("s")) %>%
head(3)
Species
1 setosa
2 setosa
3 setosa
With base
and grepl
:
head(iris[grepl("s$",names(iris),ignore.case = FALSE)])
Species
1 setosa
2 setosa
3 setosa
4 setosa
5 setosa
6 setosa
Or using purrr
:
iris %>%
purrr::keep(grepl("s$",names(.))) %>%
head()
Species
1 setosa
2 setosa
3 setosa
4 setosa
5 setosa
6 setosa
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With