Can I use dplyr::select(ends_with) to select column names that fit any of multiple conditions. Considering my column names, I want to use ends with instead of contains or matches, because the strings I want to select are relevant at the end of the column name, but may also appear in the middle in others. For instance,
df <- data.frame(a10 = 1:4,
a11 = 5:8,
a20 = 1:4,
a12 = 5:8)
I want to select columns that end with 1 or 2, to have only columns a11 and a12. Is select(ends_with) the best way to do this?
Thanks!
I don't know if
ends_with()
is the best way to do this, but you could also do this in base R with a logical index.With this index, you can subset the dataframe as follows
or with
dplyr::select()
keeping only columns that end with either 1 or 2.
From version 1.0.0, you can combine multiple selections using Boolean logic such as
!
(negate),&
(and) and|
(or).or use
num_range()
to select the desired columnsCreated on 2020-02-17 by the reprex package (v0.3.0)
You can also do this using regular expressions. I know you did not want to use matches initially, but it actually works quite well if you use the "end of string" symbol
$
. Separate your various endings with|
.If you have a more complex example with a long list, use
paste0
withcollapse = '|'
.