Reputation: 554
I have a problem parsing a series of names into first and last names. Given a list of names that looks like this:
names <- as.vector(c("Rep. Mike Grimm","House Committee on Energy and Commerce",
"Office of the Inspector General","Rep. J. Gresham Barrett","Rep. Mary Fallin"))
How would I write a regex code for sub to extract the first and last names only of the names in the list that start with "Rep."? Thus, I think I need to write a regex code that only extracts the 2nd word from names starting with "Rep." and only the 3rd word for another column, as I want to create one column for first names and another for last names. I have tried a number of regex codes, but can't find one that works. Thank you all for your help!
Upvotes: 0
Views: 43
Reputation: 28441
ans <- gsub('Rep. ', '', names[grep('Rep. ', names)])
First <- gsub('\\s\\w+$', '', ans)
Last <- gsub('.*?(\\w+$)', '\\1', ans)
df <- data.frame(First, Last)
df
# First Last
#1 Mike Grimm
#2 J. Gresham Barrett
#3 Mary Fallin
Upvotes: 1
Reputation: 819
is this what you had in mind? I wouldn't name the object as "names" because you will mask the function "names"
names.of <- as.vector(c("Rep. Mike Grimm","House Committee on Energy and Commerce",
"Office of the Inspector General","Rep. J. Gresham Barrett","Rep. Mary Fallin"))
names.rep<-grep("Rep",names.of,value=T )
gsub("Rep\\. ([A-Za-z]+)","\\1",names.rep)
Upvotes: 1