Reputation: 1946
I have the following character vector of elements that include individual's full names.
strings <- c("Michelle Jane Smith", "Elise Alice Smith", "Ronald James Smith", "Thomas John Smith")
I now wish to extract each capital letter, removing the spaces in between. This would give initials. My anticipated output would be:
output <- c("MJS", "EAS", "RJS", "TJS")
Is this please possible?
Upvotes: 0
Views: 246
Reputation: 887148
Or using capture groups
gsub("([A-Z]+)[a-z ]+", "\\1", strings)
#[1] "MJS" "EAS" "RJS" "TJS"
Upvotes: 2
Reputation: 32548
Using str_extract_all
command of stringr
package, you can extract the characters that match the pattern for uppercase letters ("[A-Z]+"
) and paste them together.
library(stringr)
sapply(str_extract_all(string = strings, pattern = "[A-Z]+"),
function(a) paste(a, collapse = ""))
#[1] "MJS" "EAS" "RJS" "TJS"
Or instead of looking for uppercase characters, you could just extract the first character of each word in strings
sapply(strsplit(x = strings, split = " "), function(a)
paste(substr(x = a, start = 1, stop = 1), collapse = ""))
#[1] "MJS" "EAS" "RJS" "TJS"
Upvotes: 2
Reputation: 26258
how about a gsub
gsub("[a-z]| ", "", strings)
[1] "MJS" "EAS" "RJS" "TJS"
Upvotes: 2