Here is an option that allows up to three words or initials between first and last names using regular expressions:
patterns <- paste0("(.*)(", signers$first, "(\\s+[[:alpha:].]+){,3}\\s+", signers$last, ")(.*)")
signers$inparagraph <- ifelse(sapply(patterns, grepl, text), "1", "")
Produces:
first last inparagraph
1 Benjamin Franklin 1
2 Thomas Mifflin
3 Robert Morris
4 George Clymer
5 Thomas Fitzsimons
6 Jared Ingersoll
7 James Wilson
8 John Blair 1
9 James Madison
10 George Washington
11 George Mason
12 James McClurg
13 Edmund Randolph 1
14 George Wythe 1
Note John Blair matches because I modified text
for testing purposes to include him (see data below). If you want to allow fewer words you can change {,3}
to a lower number. Now, if you wanted to actually extract the matched names, you could do:
unname(sapply(patterns, gsub, "\\2", text))[sapply(patterns, grepl, text)]
# [1] "Benjamin Franklin" "John W. F. Blair" "Edmund Jennings Randolph"
# [4] "George Wythe"
Here is the text
I used:
text <-
"A lot of people attended the Constitutional Convention in Philadephia, including Alexander Hamilton, Benjamin Franklin and John Adams.
Not everyone who attended the convention ended up signing the Constitution, including George Wythe, John F. Mercer and Edmund Jennings Randolph who abstained and John W. F. Blair ate cake"