Allow use of patterns in setnames when new is a function

Open
#3,918 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by reviewing setnames(), patterns(), and the related issue #3703, then compare the proposed single-function and multiple-function interpretations with patterns() usage in melt(). Define the supported matching and renaming behavior, including multiple patterns and functions, and verify it against the provided data.table example.

Written by the indexing model from the issue text.

Description

feature request

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.
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.