What I have:
names <- c("First Last", "First M Last", "First M. Last", "first Last", "first lAst")
What I want:
"FL" "FML" "FML" "FL" "FL"
What I tried:
paste(substr(strsplit(names, " ")[[1]], 1, 1), collapse="")
What this gives:
FL
How can I get this for all elements?
> names <- c("First Last", "First M Last", "First M. Last",
"first Last", "first lAst")
It looks like you want the result to be all upper case? If that's the case, we can use toupper
inside sapply
with similar code to what you've tried.
> s <- strsplit(names, " ")
> sapply(s, function(x){
toupper(paste(substring(x, 1, 1), collapse = ""))
})
# [1] "FL" "FML" "FML" "FL" "FL"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With