Search code examples
rsplitdata.tablestrsplit

Split text string in a data.table columns by location


I am trying to figure it out how I can use tstrisplit() function from data.table to split a text by location number. I am aware of the Q1, Q2 & Q3 but these do not address my question.

as an example :

 DT2 <- data.table(a = paste0(LETTERS[1:5],seq(10,15)), b = runif(6))
 DT2
     a         b  
1: A10 0.4153622
2: B11 0.1567381
3: C12 0.5361883
4: D13 0.5920144
5: E14 0.3376648
6: A15 0.5503773

I tried the following which did not work:

DT2[, c("L", "D") := tstrsplit(a, "")][]
DT2[, c("L", "D") := tstrsplit(a, "[A-Z]")][]
DT2[, c("L", "D") := tstrsplit(a, "[0-9]{1}")][]

The expectation:

     a         b    L   D
1: A10 0.4153622    A   10
2: B11 0.1567381    B   11
3: C12 0.5361883    C   12
4: D13 0.5920144    D   13
5: E14 0.3376648    E   14
6: A15 0.5503773    A   15

any help with explanation is highly appreciated.


Solution

  • You can split on regex "(?<=[A-Za-z])(?=[0-9])" if you want to split between letters and digits, (?<=[A-Za-z])(?=[0-9]) restricts the split to a position that is preceded by a letter and followed by a digit:

    The regex contains two parts, look behind (?<=[A-Za-z]) which means after a letter and look ahead (?=[0-9]), i.e before a digit, see more about regex look around, in r, you need to specify perl=TRUE to use Perl-compatible regexps to make these work:

    DT2[, c("L", "D") := tstrsplit(a, "(?<=[A-Za-z])(?=[0-9])", perl=TRUE)][]
    
    #     a          b L  D
    #1: A10 0.01487372 A 10
    #2: B11 0.95035709 B 11
    #3: C12 0.49230300 C 12
    #4: D13 0.67183871 D 13
    #5: E14 0.40076579 E 14
    #6: A15 0.27871477 A 15