Skip to content

Should num_range select helper work together with e.g. ends_with or get an additional argument "suffix = "? #229

@deschen1

Description

@deschen1

(note - copy from: tidyverse/dplyr#5764)

I was just puzzled when I tried to select certain columns of my data frame that a) start with a letter, followed by a number (for which the num_range helper would be ideal) and b) also end with a certain string, e.g. "_rec" (for which ends_with would be perfect).

However, using this approach, a data frame with 0 columns is returned. I guess that num_range is only suited for full matches, however the help function doesn't say a word about it and I also think num_range would benefit from an option to not only add a prefix, but also a suffix.

library(tidyverse)
# Data
df <- data.frame(x1 = c(1, 2),
                 x2 = c(3, 4),
                 x3 = c(5, 6),
                 x4y = c(7, 8),
                 x1_rec = c(9, 10),
                 x2_rec = c(11, 12),
                 x4y_rec = c(13, 14))

#   x1 x2 x3 x4y x1_rec x2_rec x4y_rec
# 1  1  3  5   7      9     11      13
# 2  2  4  6   8     10     12      14

# Code that should work, but doesn't
df %>% select(num_range("x", 1:2) & ends_with("_rec"))

# data frame with 0 columns and 2 rows

# Expected output
#   x1_rec x2_rec
# 1      9     11
# 2     10     12

What I could imagine is really just an extension of the num_range function, e.g.:


num_range(prefix = "x",
          range = 1:2,
          suffix = "_rec")

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions