jprice
jprice

Reputation: 5

Shorten string in R to inital of name first name.Lastname

I have a df with players names, formatted:

(Lastname, Firstname) ie: (Hill, Tyreek).

I need to get this to yield (first letter of first name. last name) ie: (T.Hill)

Thanks !

Upvotes: 0

Views: 64

Answers (2)

Chris Kenaley
Chris Kenaley

Reputation: 11

With grep/gsub . . .

n <- "Hill, Tyreek"

gsub("(\\w+), (\\w).+","\\2.\\1",n)

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522244

Just use the base string functions:

output <- paste0(substr(df$Firstname, 1, 1), '.', df$Lastname)

Upvotes: 1

Related Questions