简体   繁体   中英

Parsing in Character Vectors in R

I have a vector, superbowl$Detail , that describes what happened in each play of the 2017 Superbowl.

> str(superbowl$Detail)
 chr [1:189] "Matthew Bosher kicks off 65 yards touchback" ...
> superbowl$Detail[1:5]
[1] "Matthew Bosher kicks off 65 yards touchback"                                                                                                  
[2] "Tom Brady pass incomplete short middle intended for Julian Edelman"                                                                           
[3] "Tom Brady pass complete short right to Julian Edelman for 9 yards (tackle by Philip Wheeler)"                                                 
[4] "LeGarrette Blount right tackle for no gain (tackle by Deion Jones)"                                                                           
[5] "Ryan Allen punts 51 yards returned by Eric Weems for 1 yard (tackle by Barkevious Mingo). Penalty on Paul Worrilow: Offensive Holding 7 yards"

A second vector, superbowl$Is.Tackle , identifies those plays on which a tackle was made.

> str(superbowl$Is.Tackle)
 logi [1:189] FALSE FALSE TRUE TRUE TRUE TRUE ...
> superbowl$Is.Tackle[1:5]
[1] FALSE FALSE  TRUE  TRUE  TRUE

I would like to create a third variable, superbowl$Tackler , that identifies who made the tackle on those plays in which a tackle was made.

To do this, I've been trying a combination of ifelse() and gsub() :

> superbowl$Tackler <- ifelse(superbowl$Is.Tackle == TRUE, gsub("(\\w+\\s)*.tackle\\s(\\w+\\s)(\\w+\\s)*(\\w+\\s)*","\\3\\4",superbowl$Detail), NA)
> superbowl$Tackler[1:5]
[1] NA                                                                      
[2] NA                                                                      
[3] "Philip Wheeler)"                                                       
[4] "Deion Jones)"                                                          
[5] "Barkevious Mingo). Penalty on Paul Worrilow: Offensive Holding 7 yards"

As you can see, this function correctly calls the Tackler's name, but also includes the closing parenthesis, as well as everything after it.

I am new to R and don't fully understand the function I'm working with. How do I get it to just return the tackler's name?

( For your convenience:

> dput(superbowl[ , c(5, 45, 49)])
structure(list(Detail = c("Matthew Bosher kicks off 65 yards touchback", 
"Tom Brady pass incomplete short middle intended for Julian Edelman", 
"Tom Brady pass complete short right to Julian Edelman for 9 yards (tackle by Philip Wheeler)", 
"LeGarrette Blount right tackle for no gain (tackle by Deion Jones)", 
"Ryan Allen punts 51 yards returned by Eric Weems for 1 yard (tackle by Barkevious Mingo). Penalty on Paul Worrilow: Offensive Holding 7 yards", 
"Devonta Freeman left end for 37 yards (tackle by Malcolm Butler and Devin McCourty)", 
"Devonta Freeman left end for 3 yards (tackle by Trey Flowers and Malcom Brown)", 
"Matt Ryan pass complete short right to Patrick DiMarco for 2 yards (tackle by Patrick Chung)", 
"Matt Ryan sacked by Trey Flowers for -10 yards", "Matthew Bosher punts 55 yards returned by Julian Edelman for 5 yards (tackle by C.J. Goodwin)", 
"Julian Edelman right end for 2 yards (tackle by Keanu Neal and Deion Jones)", 
"Tom Brady pass complete short left to Danny Amendola for 13 yards (tackle by Brian Poole)", 
"Tom Brady pass complete short left to Chris Hogan for 15 yards (tackle by Jalen Collins)", 
"LeGarrette Blount left tackle for 2 yards (tackle by Keanu Neal and Brooks Reed)", 
"Tom Brady pass complete short right to Malcolm Mitchell for 7 yards (tackle by Deion Jones)", 
"Tom Brady pass complete short middle to James White for 11 yards (tackle by Jalen Collins)", 
"Tom Brady sacked by Courtney Upshaw for -8 yards", "Tom Brady pass incomplete deep left intended for James White (defended by Deion Jones)", 
"Tom Brady sacked by Grady Jarrett for -1 yards", "Ryan Allen punts 37 yards fair catch by Eric Weems", 
"Tevin Coleman right end for 9 yards (tackle by Devin McCourty)", 
"Matt Ryan pass complete short left to Patrick DiMarco for 10 yards (tackle by Patrick Chung and Logan Ryan)", 
"Devonta Freeman right tackle for 2 yards (tackle by Alan Branch and Rob Ninkovich)", 
"Tevin Coleman left end for 5 yards (tackle by Logan Ryan)", 
"Matt Ryan sacked by Jabaal Sheard and Alan Branch for -2 yards", 
"Matthew Bosher punts 55 yards returned by Julian Edelman for 9 yards (tackle by Eric Weems)", 
"Tom Brady pass complete short left to Julian Edelman for 13 yards (tackle by Robert Alford)", 
"LeGarrette Blount middle for 7 yards (tackle by Robert Alford and Ricardo Allen)", 
"LeGarrette Blount middle for 2 yards (tackle by De'Vondre Campbell)", 
"Tom Brady pass complete deep right to Julian Edelman for 27 yards (tackle by Robert Alford)", 
"LeGarrette Blount left tackle for 4 yards (tackle by Deion Jones). LeGarrette Blount fumbles (forced by Deion Jones) recovered by Robert Alford at ATL-29 (tackle by Julian Edelman). Penalty on Martellus Bennett: Offensive Holding (Declined)", 
"Matt Ryan pass complete short middle to Julio Jones for 19 yards (tackle by Logan Ryan)", 
"Matt Ryan pass complete deep left to Julio Jones for 23 yards", 
"Devonta Freeman right tackle for 15 yards (tackle by Devin McCourty)", 
"Devonta Freeman left tackle for 9 yards (tackle by Duron Harmon)", 
"Timeout #1 by New England Patriots", "Devonta Freeman left end for 5 yards touchdown", 
"Matt Bryant kicks extra point good", "Matthew Bosher kicks off 69 yards returned by Dion Lewis for 20 yards (tackle by Sharrod Neasman)", 
"Tom Brady pass incomplete deep right intended for Malcolm Mitchell (defended by C.J. Goodwin)", 
"Dion Lewis middle for 3 yards (tackle by Ricardo Allen)", "Tom Brady pass complete short left to James White for 5 yards (tackle by C.J. Goodwin)", 
"Ryan Allen punts 38 yards", "Matt Ryan pass complete deep right to Taylor Gabriel for 24 yards (tackle by Devin McCourty)", 
"Matt Ryan pass complete deep left to Julio Jones for 18 yards (tackle by Eric Rowe)", 
"Tevin Coleman right tackle for 1 yard (tackle by Alan Branch)", 
"Matt Ryan pass incomplete deep right intended for Austin Hooper (defended by Patrick Chung)", 
"Matt Ryan pass complete deep left to Austin Hooper for 19 yards touchdown. Penalty on Patrick Chung: Defensive Pass Interference (Declined)", 
"Penalty on Shea McClellin: Illegal Formation 5 yards (no play)", 
"Matt Bryant kicks extra point good", "Matthew Bosher kicks off 65 yards touchback", 
"Tom Brady pass complete short middle to Martellus Bennett for 12 yards (tackle by Jalen Collins and Keanu Neal)", 
"Tom Brady pass incomplete short middle intended for Julian Edelman", 
"Tom Brady pass incomplete deep middle intended for Danny Amendola", 
"Tom Brady pass complete short right to James White for 8 yards (tackle by Deion Jones and De'Vondre Campbell). Penalty on Robert Alford: Defensive Holding 5 yards (no play)", 
"LeGarrette Blount left tackle for no gain (tackle by Joe Vellano and Jonathan Babineaux)", 
"James White right end for 7 yards (tackle by Robert Alford)", 
"Tom Brady pass incomplete deep right intended for Julian Edelman. Penalty on Brian Poole: Defensive Holding 5 yards (no play)", 
"LeGarrette Blount right end for 1 yard (tackle by Tyson Jackson)", 
"Tom Brady pass incomplete short right intended for Dion Lewis", 
"Timeout #1 by Atlanta Falcons", "Tom Brady pass incomplete short middle intended for Julian Edelman. Penalty on Brian Poole: Defensive Holding 5 yards (no play)", 
"LeGarrette Blount middle for no gain (tackle by Ra'Shede Hageman)", 
"Tom Brady pass complete short middle to Martellus Bennett for 13 yards (tackle by Ayodeji Olatoye)", 
"Dion Lewis right guard for 4 yards (tackle by De'Vondre Campbell)", 
"Dion Lewis left guard for no gain (tackle by Grady Jarrett)", 
"Tom Brady pass incomplete short left intended for Danny Amendola is intercepted by Robert Alford at ATL-18 and returned for 82 yards touchdown", 
"Matt Bryant kicks extra point good", "Matthew Bosher kicks off 65 yards touchback", 
"James White right guard for 3 yards (tackle by Ra'Shede Hageman and Robert Alford)", 
"Tom Brady pass complete short left to Martellus Bennett for 15 yards (tackle by Keanu Neal and Ricardo Allen)", 
"Timeout #2 by Atlanta Falcons", "Tom Brady pass incomplete deep right intended for Julian Edelman", 
"Tom Brady pass complete short right to James White for 28 yards (tackle by Ricardo Allen)", 
"Tom Brady pass complete short right to Chris Hogan for 8 yards (tackle by Keanu Neal)", 
"Tom Brady pass incomplete short right intended for Chris Hogan", 
"Tom Brady pass complete short middle to James White for 6 yards (tackle by Deion Jones)", 
"Timeout #2 by New England Patriots", "Tom Brady pass incomplete short left intended for Julian Edelman (defended by Robert Alford)", 
"Tom Brady pass complete short left to James White for 5 yards (tackle by Keanu Neal). Penalty on Martellus Bennett: Offensive Holding 10 yards", 
"Tom Brady pass complete short left to Martellus Bennett for -3 yards (tackle by Ayodeji Olatoye and Grady Jarrett)", 
"Stephen Gostkowski 41 yard field goal good", "Timeout #3 by New England Patriots", 
"Stephen Gostkowski kicks off 38 yards", "Stephen Gostkowski kicks off 60 yards returned by Eric Weems for 14 yards (tackle by Nate Ebner)", 
"Devonta Freeman left tackle for -3 yards (tackle by Dont'a Hightower)", 
"Matt Ryan pass complete short middle to Devonta Freeman for 7 yards (tackle by Logan Ryan)", 
"Matt Ryan pass incomplete short left intended for Taylor Gabriel (defended by Eric Rowe)", 
"Matthew Bosher punts 56 yards returned by Julian Edelman for 26 yards (tackle by C.J. Goodwin)", 
"ATL challenged the runner was in bounds ruling and the play was overturned. Matthew Bosher punts 56 yards returned by Julian Edelman for 26 yards (tackle by Eric Weems)", 
"Tom Brady pass incomplete deep left intended for Chris Hogan", 
"Tom Brady pass complete short right to Danny Amendola for -2 yards (tackle by Brian Poole). Penalty on Chris Hogan: Offensive Pass Interference (Declined)", 
"Timeout #1 by Atlanta Falcons", "Tom Brady pass incomplete short middle intended for Julian Edelman", 
"Ryan Allen punts 40 yards fair catch by Eric Weems", "Matt Ryan pass complete short middle to Taylor Gabriel for 17 yards (tackle by Eric Rowe)", 
"Tevin Coleman right tackle for 5 yards (tackle by Elandon Roberts)", 
"Matt Ryan pass complete deep middle to Taylor Gabriel for 35 yards (tackle by Duron Harmon)", 
"Tevin Coleman left end for no gain (tackle by Patrick Chung)", 
"Matt Ryan pass complete short middle to Mohamed Sanu for 13 yards (tackle by Eric Rowe)", 
"Devonta Freeman right tackle for 9 yards (tackle by Trey Flowers)", 
"Devonta Freeman middle for -3 yards (tackle by Eric Rowe and Rob Ninkovich)", 
"Matt Ryan pass incomplete short middle intended for Taylor Gabriel (defended by Malcolm Butler). Penalty on Malcolm Butler: Defensive Pass Interference 3 yards (no play)", 
"Matt Ryan pass complete short right to Tevin Coleman for 6 yards touchdown", 
"Matt Bryant kicks extra point good", "Matthew Bosher kicks off 65 yards touchback", 
"Tom Brady pass complete short middle to Dion Lewis for 2 yards (tackle by Keanu Neal)", 
"Tom Brady pass complete short left to James White for 12 yards (tackle by Jalen Collins and Ricardo Allen)", 
"Dion Lewis middle for 8 yards (tackle by Jalen Collins)", "Dion Lewis middle for -1 yards (tackle by Brooks Reed)", 
"Julian Edelman pass incomplete deep right intended for Dion Lewis", 
"Tom Brady pass complete short left to Danny Amendola for 17 yards (tackle by De'Vondre Campbell)", 
"Tom Brady pass complete short left to Danny Amendola for 2 yards (tackle by Jalen Collins)", 
"Tom Brady pass incomplete short left intended for Julian Edelman", 
"Tom Brady middle for 15 yards (tackle by Robert Alford)", "LeGarrette Blount right guard for 4 yards (tackle by Keanu Neal)", 
"LeGarrette Blount right tackle for 9 yards (tackle by Robert Alford)", 
"LeGarrette Blount middle for 2 yards (tackle by Brooks Reed)", 
"Tom Brady pass complete short left to James White for 5 yards touchdown", 
"Stephen Gostkowski kicks extra point no good", "Stephen Gostkowski kicks onside 11 yards recovered by LaRoy Reynolds. Penalty on Stephen Gostkowski: Illegal Touch Kick 5 yards", 
"Matt Ryan pass complete short left to Austin Hooper for 9 yards (tackle by Duron Harmon)", 
"Tevin Coleman left tackle for -1 yards (tackle by Trey Flowers). Penalty on Jake Matthews: Offensive Holding 10 yards (no play)", 
"Timeout #2 by Atlanta Falcons", "Matt Ryan pass incomplete short right intended for Austin Hooper (defended by Patrick Chung)", 
"--", "Penalty on Matthew Bosher: Delay of Game 5 yards (no play)", 
"Matthew Bosher punts 42 yards returned by Patrick Chung for -1 yards (tackle by Justin Hardy)", 
"Tom Brady pass complete deep right to Malcolm Mitchell for 15 yards (tackle by Jalen Collins)", 
"Tom Brady pass complete short middle to Malcolm Mitchell for 7 yards (tackle by Jalen Collins)", 
"James White middle for 6 yards (tackle by Keanu Neal)", "Tom Brady pass incomplete deep left intended for Julian Edelman", 
"Tom Brady pass complete short right to Malcolm Mitchell for 18 yards (tackle by Robert Alford)", 
"Tom Brady pass complete short right to James White for 9 yards (tackle by Robert Alford)", 
"Tom Brady pass incomplete short middle intended for Danny Amendola", 
"Tom Brady pass complete deep right to Martellus Bennett for 25 yards (tackle by Keanu Neal)", 
"Tom Brady sacked by Grady Jarrett for -5 yards", "Tom Brady pass complete short left to James White for 2 yards (tackle by Jalen Collins and De'Vondre Campbell)", 
"Tom Brady sacked by Grady Jarrett for -5 yards", "Stephen Gostkowski 33 yard field goal good", 
"Stephen Gostkowski kicks off 48 yards returned by Justin Hardy for 10 yards (tackle by Barkevious Mingo)", 
"Tevin Coleman right end for 8 yards (tackle by Patrick Chung)", 
"Tevin Coleman middle for 1 yard (tackle by Trey Flowers and Logan Ryan)", 
"Matt Ryan sacked by Dont'a Hightower for -11 yards. Matt Ryan fumbles (forced by Dont'a Hightower) recovered by Alan Branch at ATL-25 (tackle by Chris Chester)", 
"Tom Brady sacked by Dwight Freeney for -5 yards", "Tom Brady pass complete short middle to James White for 4 yards (tackle by Keanu Neal)", 
"Tom Brady pass complete short left to Malcolm Mitchell for 12 yards (tackle by C.J. Goodwin)", 
"Tom Brady pass complete short left to Danny Amendola for 8 yards (tackle by Ricardo Allen)", 
"Tom Brady pass complete short left to Danny Amendola for 6 yards touchdown", 
"Two Point Attempt: James White middle conversion succeeds", 
"Stephen Gostkowski kicks off 62 yards returned by Justin Hardy for 7 yards (tackle by Jonathan Jones)", 
"Matt Ryan pass complete short left to Devonta Freeman for 39 yards (tackle by Elandon Roberts)", 
"Devonta Freeman right end for 2 yards (tackle by Jabaal Sheard and Patrick Chung)", 
"Matt Ryan pass complete deep right to Julio Jones for 27 yards", 
"Devonta Freeman left end for -1 yards (tackle by Devin McCourty)", 
"Matt Ryan sacked by Trey Flowers for -12 yards", "Timeout #1 by New England Patriots", 
"Matt Ryan pass complete short left to Mohamed Sanu for 9 yards (tackle by Logan Ryan). Penalty on Jake Matthews: Offensive Holding 10 yards (no play)", 
"Matt Ryan pass incomplete short left intended for Taylor Gabriel", 
"Matthew Bosher punts 36 yards fair catch by Julian Edelman", 
"Tom Brady pass incomplete short right intended for James White", 
"Tom Brady pass incomplete deep right intended for Chris Hogan", 
"Tom Brady pass complete short right to Chris Hogan for 16 yards (tackle by Jalen Collins)", 
"Tom Brady pass incomplete short middle intended for Julian Edelman (defended by Robert Alford)", 
"Tom Brady pass complete short left to Malcolm Mitchell for 11 yards (tackle by Jalen Collins)", 
"Tom Brady pass complete deep middle to Julian Edelman for 23 yards (tackle by Keanu Neal)", 
"ATL challenged the pass completion ruling and the play was upheld.", 
"Tom Brady pass complete deep right to Danny Amendola for 20 yards (tackle by Brian Poole)", 
"Tom Brady pass complete short right to James White for 13 yards (tackle by Brian Poole and Ricardo Allen)", 
"Tom Brady pass complete short right to James White for 7 yards (tackle by Deion Jones)", 
"James White right guard for 1 yard touchdown", "Two Point Attempt: Tom Brady pass complete to Danny Amendola conversion succeeds. Penalty on Dwight Freeney: Defensive Offside 5 yards", 
"Stephen Gostkowski kicks off 60 yards returned by Eric Weems for 11 yards (tackle by Brandon Bolden)", 
"Matt Ryan pass complete short left to Mohamed Sanu for 12 yards (tackle by Logan Ryan)", 
"Matt Ryan pass complete short right to Austin Hooper for 4 yards (tackle by Malcolm Butler)", 
"Matt Ryan spiked the ball", "Matt Ryan pass incomplete deep left intended for Austin Hooper", 
"Matthew Bosher punts 38 yards fair catch by Julian Edelman", 
"Dion Lewis for 13 yards", "Matthew Bosher kicks off 65 yards touchback", 
"Tom Brady pass complete short left to James White for 6 yards (tackle by Deion Jones)", 
"Tom Brady pass complete short right to Danny Amendola for 14 yards", 
"Tom Brady pass complete short left to Chris Hogan for 18 yards (tackle by Keanu Neal and Deion Jones)", 
"Tom Brady pass complete short left to James White for -3 yards (tackle by Deion Jones)", 
"Tom Brady pass complete short left to Julian Edelman for 15 yards (tackle by Robert Alford)", 
"James White right end for 10 yards (tackle by Robert Alford)", 
"Tom Brady pass incomplete short right intended for Martellus Bennett (defended by De'Vondre Campbell). Penalty on De'Vondre Campbell: Defensive Pass Interference 13 yards (no play)", 
"Tom Brady pass incomplete short right intended for Martellus Bennett (defended by Vic Beasley)", 
"James White right end for 2 yards touchdown"), Is.Tackle = c(FALSE, 
FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, 
TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, 
TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, 
TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, 
FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, 
FALSE, FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, 
FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, 
TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, 
TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, 
TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, TRUE, 
FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, 
TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, 
TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, 
FALSE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, 
FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE, 
FALSE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE), Tackler = c(NA, 
NA, "Philip Wheeler)", "Deion Jones)", "Barkevious Mingo). Penalty on Paul Worrilow: Offensive Holding 7 yards", 
"Devin McCourty)", "Malcom Brown)", "Patrick Chung)", NA, "C.J. Goodwin)", 
"Deion Jones)", "Brian Poole)", "Jalen Collins)", "Brooks Reed)", 
"Deion Jones)", "Jalen Collins)", NA, NA, NA, NA, "Devin McCourty)", 
"Logan Ryan)", "Rob Ninkovich)", "Logan Ryan)", NA, "Eric Weems)", 
"Robert Alford)", "Ricardo Allen)", "De'Vondre Campbell)", "Robert Alford)", 
"Deion Jones). LeGarrette Blount fumbles (forced by Deion Jones) recovered by Robert Alford at ATL-Julian Edelman). Penalty on Martellus Bennett: Offensive Holding (Declined)", 
"Logan Ryan)", NA, "Devin McCourty)", "Duron Harmon)", NA, NA, 
NA, "Sharrod Neasman)", NA, "Ricardo Allen)", "C.J. Goodwin)", 
NA, "Devin McCourty)", "Eric Rowe)", "Alan Branch)", NA, NA, 
NA, NA, NA, "Keanu Neal)", NA, NA, "and De'Vondre Campbell). Penalty on Robert Alford: Defensive Holding 5 yards (no play)", 
"Jonathan Babineaux)", "Robert Alford)", NA, "Tyson Jackson)", 
NA, NA, NA, "Ra'Shede Hageman)", "Ayodeji Olatoye)", "De'Vondre Campbell)", 
"Grady Jarrett)", NA, NA, NA, "Ra'Shede Hageman and Robert Alford)", 
"Ricardo Allen)", NA, NA, "Ricardo Allen)", "Keanu Neal)", NA, 
"Deion Jones)", NA, NA, "Keanu Neal). Penalty on Martellus Bennett: Offensive Holding 10 yards", 
"Tom Brady pass complete short left to Martellus Bennett for -Grady Jarrett)", 
NA, NA, NA, "Nate Ebner)", "Devonta Freeman left-Dont'a Hightower)", 
"Logan Ryan)", NA, "C.J. Goodwin)", "ATL challenged the runner was in bounds ruling and the play was overturned. Eric Weems)", 
NA, "Tom Brady pass complete short right to Danny Amendola for -Brian Poole). Penalty on Chris Hogan: Offensive Pass Interference (Declined)", 
NA, NA, NA, "Eric Rowe)", "Elandon Roberts)", "Duron Harmon)", 
"Patrick Chung)", "Eric Rowe)", "Trey Flowers)", "Devonta Freeman middle for -Rob Ninkovich)", 
NA, NA, NA, NA, "Keanu Neal)", "Ricardo Allen)", "Jalen Collins)", 
"Dion Lewis middle for -Brooks Reed)", NA, "De'Vondre Campbell)", 
"Jalen Collins)", NA, "Robert Alford)", "Keanu Neal)", "Robert Alford)", 
"Brooks Reed)", NA, NA, NA, "Duron Harmon)", "Tevin Coleman left-Trey Flowers). Penalty on Jake Matthews: Offensive Holding 10 yards (no play)", 
NA, NA, NA, NA, "Matthew Bosher punts 42 yards returned by Patrick Chung for -Justin Hardy)", 
"Jalen Collins)", "Jalen Collins)", "Keanu Neal)", NA, "Robert Alford)", 
"Robert Alford)", NA, "Keanu Neal)", NA, "and De'Vondre Campbell)", 
NA, NA, "Barkevious Mingo)", "Patrick Chung)", "Logan Ryan)", 
"Matt Ryan sacked by Dont'a Hightower for -11 yards. Matt Ryan fumbles (forced by Dont'a Hightower) recovered by Alan Branch at ATL-Chris Chester)", 
NA, "Keanu Neal)", "C.J. Goodwin)", "Ricardo Allen)", NA, NA, 
"Jonathan Jones)", "Elandon Roberts)", "Patrick Chung)", NA, 
"Devonta Freeman left end for -Devin McCourty)", NA, NA, "Logan Ryan). Penalty on Jake Matthews: Offensive Holding 10 yards (no play)", 
NA, NA, NA, NA, "Jalen Collins)", NA, "Jalen Collins)", "Keanu Neal)", 
NA, "Brian Poole)", "Ricardo Allen)", "Deion Jones)", NA, NA, 
"Brandon Bolden)", "Logan Ryan)", "Malcolm Butler)", NA, NA, 
NA, NA, NA, "Deion Jones)", NA, "Deion Jones)", "Tom Brady pass complete short left to James White for -Deion Jones)", 
"Robert Alford)", "Robert Alford)", NA, NA, NA)), .Names = c("Detail", 
"Is.Tackle", "Tackler"), class = "data.frame", row.names = c(NA, 
-189L))

)

Thanks for your help!

EDIT

> str <- str_extract(superbowl$Detail, "\\(tackle.+\\)")
> superbowl$Tackler <- str_extract(str, "\\w+ \\w+\\)")
> superbowl$Tackler <- str_replace(superbowl$Tackler, "\\)", "")
> superbowl$Tackler[1:5]
[1] NA                 NA                 "Philip Wheeler"  
[4] "Deion Jones"      "Barkevious Mingo"
> superbowl[c(10, 42, 89, 147), c(5, 46, 51)]
                                                                                            Detail
10   Matthew Bosher punts 55 yards returned by Julian Edelman for 5 yards (tackle by C.J. Goodwin)
42          Tom Brady pass complete short left to James White for 5 yards (tackle by C.J. Goodwin)
89  Matthew Bosher punts 56 yards returned by Julian Edelman for 26 yards (tackle by C.J. Goodwin)
147   Tom Brady pass complete short left to Malcolm Mitchell for 12 yards (tackle by C.J. Goodwin)
    Is.Tackle Tackler
10       TRUE    <NA>
42       TRUE    <NA>
89       TRUE    <NA>
147      TRUE    <NA>

Here is a quick solution using the stringr package.

plays<-c("Matthew Bosher kicks off 65 yards touchback", 
         "Tom Brady pass incomplete short middle intended for Julian Edelman", 
         "Tom Brady pass complete short right to Julian Edelman for 9 yards (tackle by Philip Wheeler)", 
         "LeGarrette Blount right tackle for no gain (tackle by Deion Jones)")

library(stringr)
#find lines with the pattern "(tackles....)" identified
str<-str_extract(plays, "\\(tackle.+?\\)")
#find the last 2 words before the closing )
tacklers<-str_extract(str, "\\w+ \\w+\\)")
#replace ) with a blank
tacklers<-str_replace(tacklers, "\\)", "")

The stringr package has a more consistent interface then the grep and gsub commands

Edit: If the play is made by more than one player or initials are involve as per your comment try:

str<-str_extract(plays, "\\(tackle.+?\\)")
tacklers<-str_sub(str, start=12, end=str_length(str)-1)
tacklers  

I had to add the ? into the string extract statement in order to make the regex statement non-greedy in to avoid plays where a penalty was involved. There is still one potential problem with line 31 where only the first of the two tackles are retrieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM