Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
deschen1 opened this issue Feb 16, 2021 · 4 comments · Fixed by #286
Labels
feature a feature request or enhancement

Comments

@deschen1
Copy link

(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")
@lionel- lionel- added the feature a feature request or enhancement label Mar 3, 2022
@lionel-
Copy link
Member

lionel- commented Mar 3, 2022

Thanks for the suggestion. In this case I'd rather not add features to num_range() given that matches() provides enough flexibility.

@lionel- lionel- closed this as completed Mar 3, 2022
@deschen1
Copy link
Author

deschen1 commented Mar 9, 2022

Understood, but in this case there's still the remaining bug (IMO) that the combination of num_range with other select helpers does not work as e.g. shown above with:

df %>% select(num_range("x", 1:2) & ends_with("_rec"))

So I'd hope for a respective re-opening of the issue.

Not sure though if that is a tidyselect problem or a dplyr/select problem.

@lionel-
Copy link
Member

lionel- commented Mar 14, 2022

ok reopening so I can take a look at your reprex when I get back to work on tidyselect.

@lionel- lionel- reopened this Mar 14, 2022
@hadley
Copy link
Member

hadley commented Aug 12, 2022

library(dplyr, warn.conflicts = FALSE)

df <- tibble(
  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)
)

df %>% select(num_range("x", 1:2)) |> names()
#> [1] "x1" "x2"
df %>% select(ends_with("_rec")) |> names()
#> [1] "x1_rec"  "x2_rec"  "x4y_rec"

Created on 2022-08-12 by the reprex package (v2.0.1)

It's because num_range() only returns x1 and x2. I think it's probably fine to add a suffix argument to num_range().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants