-1

I have the following data.table

library(data.table)

dt <- data.table(c('Female 18 - 24', 'Male 18+', 'People 18 -54'))

How can I transform it to

dt <- data.table(c('F 18 - 24', 'M 18+', 'P 18 -54')) using regex ?

quant
  • 4,062
  • 5
  • 29
  • 70

2 Answers2

2
 gsub('(^[A-Z])[A-Za-z]+\\s(*.)','\\1 \\2',c('Female 18 - 24', 'Male 18+', 'People 18 -54'))
 [1] "F 18 - 24" "M 18+"     "P 18 -54" 

  • (^[A-Z]) take the first letter as group1
  • [A-Za-z]+\\s any number of small letters follwed by a space
  • (*.) take any thing come after a space as group2
  • '\\1 \\2' Finally, return group1 and group2 separated by a space
  • A. Suliman
    • 12,923
    • 5
    • 24
    • 37
    0
    require(data.table)
    require(stringi)
    dt <- data.table(V1=c('Female 18 - 24', 'Male 18+', 'People 18 -54'))
    dt[,V2:= stri_replace_first_regex(V1,"(?<=\\w)\\w+","")]  
    dt
    
               V1        V2
    1: Female 18 - 24    F 18 - 24
    2: Male 18+          M 18+
    3: People 18 -54     P 18 -54
    
    asepsiswu
    • 11
    • 2
    • Don't use `require` when you just want to load the package. Use `library`. See [this SO answer](https://stackoverflow.com/questions/5595512/what-is-the-difference-between-require-and-library/51263513) for more details. – phiver Oct 27 '18 at 15:19