Rdatatable / data.table

R's data.table package extends data.frame:
http://r-datatable.com
Mozilla Public License 2.0
3.6k stars 984 forks source link

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

Open smingerson opened 5 years ago

smingerson commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

@smingerson I think returning gracefully will be better