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
?
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
?
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 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