Skip to content

Feature Request: tstrsplit(... keep) additional options #6341

Description

@JBrownArcGen

The tstrsplit function has improved efficiency in a number of transform operations I have implemented and the keep parameter in particular has streamlined the process of getting string components into fields of the data.table.

At the moment keep can be applied by index which allows components to be accessed based on an anchor-point at the start of the string being split. It would be very useful to be able to anchor the keep to the end of the string as well because sometimes you won't know how many components are created, you only know you want the last one or last but one, or similar.

This outcome can be achieved at the moment using the standard strsplit, rev and lapply functions, combined with data.table::transpose, but is somewhat awkward to use in some situations:

x <- c("ABC-DEF", "ABC-DEF-GHI", "ABC-DEF-GHI-JKL", "ABC-DEF-GHI-JKL-MNO")
data.table::transpose(lapply(strsplit(x, "-", fixed = TRUE), rev))[1:3]

> [[1]]
> [1] "DEF" "GHI" "JKL" "MNO"
> 
> [[2]]
> [1] "ABC" "DEF" "GHI" "JKL"
> 
> [[3]]
> [1] NA    "ABC" "DEF" "GHI"

It would be valuable to be able to implement this "reversed" outcome natively as a parameter to tstrsplit itself, so something like:

data.table::tstrsplit(x, "-", fixed = TRUE, keep = 1:3, rev = TRUE)

> [[1]]
> [1] "DEF" "GHI" "JKL" "MNO"
> 
> [[2]]
> [1] "ABC" "DEF" "GHI" "JKL"
> 
> [[3]]
> [1] NA   "ABC" "DEF" "GHI"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions