Extract the first letter of each word in string via R

22 November 2022

library(data.table)

# example 

>     head(data[, .N, FARBE])

            FARBE  N
1: Royal / Indigo  1
2: Red / Dark Red  3
3: Dark Red / Red  2
4:          Black 51
5:       Sea Foam 13
6:      Navy Blue  3

>     data[, CC := gsub('\\b(\\pL)\\pL{2,}|.', '\\U\\1', FARBE, perl = TRUE)] # if 2 words get first letter

>     head(data[, .N, .(FARBE, CC)])

            FARBE  CC  N
1: Royal / Indigo  RI  1
2: Red / Dark Red RDR  3
3: Dark Red / Red DRR  2
4:          Black   B 51
5:       Sea Foam  SF 13
6:      Navy Blue  NB  3


Comments

    Join the discussion for this article on this ticket. Comments appear on this page instantly.
    Thanks to aristaht for making this static comment system possible.