I have a list of names that look like this:
c("CASEY Aoife", "CREMEN Margaret", "MORCH-PEDERSEN Marie",
"RORVIK Jenny Marie", "MIGUEL GOMES Natalia", "ROHNER Maria-Clara")
and to display them in a table I would like them to look like this
c("A. CASEY", "M. CREMEN", "M. MORCH-PEDERSEN",
"J. RORVIK", "N. MIGUEL GOMES", "M. ROHNER")
There are challenges as there are people with multiple first names and multiple last names etc, as well as dealing with hyphens etc.
I've tried a function as below but not getting my desired output:
convert_name <- function(name) {
parts <- str_split(name, " ")[[1]] # Split name into parts
# Extract initials and last name
initials <- str_extract(parts, "\\b\\p{L}") # Extract first letter of each part
last_name <- parts[length(parts)]
# Concatenate initials and last name with space
converted_name <- paste(initials, last_name, sep = ". ")
return(converted_name)
}
sapplyover each name a function to shuffle the name.Another option without forloop:
Output