'keep' argument for tstrsplit?

Open
#5,250 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
r
Domain
data

Research direction

Start by reading the existing tstrsplit entry point and its current argument handling. Compare the requested keep behavior with the examples in the issue, then determine the API naming and edge cases before adding coverage that verifies selected components are returned without unnecessary splitting.

Written by the indexing model from the issue text.

Description

feature request

Often when "splitting wide" a string column, the original string has some throwaway components. The current API forces us to either write clunky code or transpose inefficiently:

NN = 1e7
DT = data.table(str = paste0(sample(letters, NN, TRUE), "_", sample(letters, NN, TRUE), "_col"), v = rnorm(NN))

microbenchmark::microbenchmark(
  times = 5L,
  `1` = DT[, tstrsplit(str, "_", fixed = TRUE)[1:2]],
  `2` = DT[, transpose(lapply(strsplit(str, "_", fixed = TRUE), `[`, 1:2))],
  `3` = DT[, tstrsplit(gsub("_col", "", str, fixed = TRUE), "_", fixed = TRUE)],
  # showing time for pure strsplit()
  `4` = strsplit(DT$str, "_", fixed = TRUE)
  # desired
  # DT[, tstrsplit(str, "_", fixed = TRUE, keep = 1:2)]
)
# Unit: seconds
#  expr       min        lq      mean    median        uq      max neval
#     1  3.534707  6.066836  9.725128 11.329644 12.618828 15.07563     5
#     2 10.906799 15.585412 18.248300 18.809941 21.567817 24.37153     5
#     3  6.978639  7.204081  8.011450  7.646223  7.912002 10.31630     5
#     4  3.460360  5.331546  5.978603  5.615473  6.288924  9.19671     5

(open to suggestions on argument name/API)

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.