Reputation: 10270
I am trying to add a column to a data set in R. The column should be the initials from a name column. I am trying to use lapply
and passing in a function to get the initials - however, I can't get this regexp to work.
pattern <- "(\b[a-zA-Z])"
str<-"MICHAEL, JENSON F"
m <- regexpr(pattern,str,perl=TRUE)
regmatches(str,m)
Returns character(0)
How can I have R return a list of matches of a string? I want regmatches to return M J and F.
Upvotes: 0
Views: 1679
Reputation: 48201
There are two problems: \b
must be escaped and you should use gregexpr
instead of regexpr
because the latter returns only the first match.
pattern <- "(\\b[a-zA-Z])"
str<-"MICHAEL, JENSON F"
m <- gregexpr(pattern,str,perl=TRUE)
regmatches(str,m)[[1]]
# [1] "M" "J" "F"
Upvotes: 4
Reputation: 10270
Just figured out the stringr library.
str_match_all(str, "(\\b[a-zA-Z])[a-zA-Z]* ?")
Upvotes: 2