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

Allow use of patterns in setnames when new is a function #3918

Open
smingerson opened this issue Sep 28, 2019 · 2 comments
Open

Allow use of patterns in setnames when new is a function #3918

smingerson opened this issue Sep 28, 2019 · 2 comments

Comments

@smingerson
Copy link

smingerson commented Sep 28, 2019

Allow the use of setnames(old = patterns(...), new = renaming_function)
Similar to #3703, this makes setnames() even more convenient to use.

# Example
dt = data.table(x = 1:5, a_x = 10:14, a_y = runif(5), z = rnorm(5))
setnames(dt, patterns("^a|z"), toupper)
dt
       x   A_X        A_Y          Z
   <int> <int>      <num>      <num>
1:     1    10 0.20489873 -0.5543310
2:     2    11 0.53906528 -0.8251313
3:     3    12 0.06511412  0.5853826
4:     4    13 0.03495321  0.8802074
5:     5    14 0.24168753 -0.5633140

One question with this is how patterns() would be used in setnames(). The two options that sprang to my mind:

  1. Matches from multiple patterns are concatenated, and only one renaming function may be provided.
  2. patterns in setnames() operates similar to melt(), where multiple patterns can be provided to match different names given in the value.name argument. The first pattern would match the first in a list of functions passed to new, and so on. If only one function was passed, the list would be concatenated or error, depending on preference.
@smingerson
Copy link
Author

smingerson commented Oct 11, 2019

I put together a first try for behavior 1 above here.

One question is whether skip_absent should let setnames() return gracefully instead of erroring if no names are matched by patterns.

@jangorecki
Copy link
Member

@smingerson I think returning gracefully will be better

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

No branches or pull requests

2 participants