rregexgsubqdap

Extract values based on last n characters


I have a vector like below:

vector 
jdjss-jdhs--abc-bec-ndj
kdjska-kvjd-jfj-nej-ndjk
eknd-nend-neekd-nemd-nemdkd-nedke

How do I extract the last 3 values so that my result looks like below based on a - delimitor:

vector                              Col1     Col2    Col3
jdjss-jdhs--abc-bec-ndj              abc      bec     ndj   
kdjska-kvjd-jfj-nej-ndjk             jfj      nej    ndjk
eknd-nend-neekd-nemd-nemdkd-nedke   nemd   nemdkd   nedke

I've attemped to use sub and the qdap package but no luck.

sub( "(^[^-]+[-][^-]+)(.+$)", "\\2", df$vector)
qdap::char2end(df$vector, "-", 3)

Not sure how to go about doing this.


Solution

  • strcapture, as a base R corollary to the tidyr extract answer from Wiktor:

    strcapture("([^-]*)-([^-]*)-([^-]*)$", df$vector, proto=list(Col1="",Col2="",Col3=""))
    #  Col1   Col2  Col3
    #1  abc    bec   ndj
    #2  jfj    nej  ndjk
    #3 nemd nemdkd nedke