Parsing in Character Vectors in R -


i have vector, superbowl$detail, describes happened in each play of 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 julian edelman"                                                                            [3] "tom brady pass complete short right julian edelman 9 yards (tackle philip wheeler)"                                                  [4] "legarrette blount right tackle no gain (tackle deion jones)"                                                                            [5] "ryan allen punts 51 yards returned eric weems 1 yard (tackle barkevious mingo). penalty on paul worrilow: offensive holding 7 yards" 

a second vector, superbowl$is.tackle, identifies plays on tackle 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 create third variable, superbowl$tackler, identifies made tackle on plays in tackle made.

to this, i've been trying combination of ifelse() , 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 can see, function correctly calls tackler's name, includes closing parenthesis, after it.

i new r , don't understand function i'm working with. how return tackler's name?

( 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 julian edelman",  "tom brady pass complete short right julian edelman 9 yards (tackle philip wheeler)",  "legarrette blount right tackle no gain (tackle deion jones)",  "ryan allen punts 51 yards returned eric weems 1 yard (tackle barkevious mingo). penalty on paul worrilow: offensive holding 7 yards",  "devonta freeman left end 37 yards (tackle malcolm butler , devin mccourty)",  "devonta freeman left end 3 yards (tackle trey flowers , malcom brown)",  "matt ryan pass complete short right patrick dimarco 2 yards (tackle patrick chung)",  "matt ryan sacked trey flowers -10 yards", "matthew bosher punts 55 yards returned julian edelman 5 yards (tackle c.j. goodwin)",  "julian edelman right end 2 yards (tackle keanu neal , deion jones)",  "tom brady pass complete short left danny amendola 13 yards (tackle brian poole)",  "tom brady pass complete short left chris hogan 15 yards (tackle jalen collins)",  "legarrette blount left tackle 2 yards (tackle keanu neal , brooks reed)",  "tom brady pass complete short right malcolm mitchell 7 yards (tackle deion jones)",  "tom brady pass complete short middle james white 11 yards (tackle jalen collins)",  "tom brady sacked courtney upshaw -8 yards", "tom brady pass incomplete deep left intended james white (defended deion jones)",  "tom brady sacked grady jarrett -1 yards", "ryan allen punts 37 yards fair catch eric weems",  "tevin coleman right end 9 yards (tackle devin mccourty)",  "matt ryan pass complete short left patrick dimarco 10 yards (tackle patrick chung , logan ryan)",  "devonta freeman right tackle 2 yards (tackle alan branch , rob ninkovich)",  "tevin coleman left end 5 yards (tackle logan ryan)",  "matt ryan sacked jabaal sheard , alan branch -2 yards",  "matthew bosher punts 55 yards returned julian edelman 9 yards (tackle eric weems)",  "tom brady pass complete short left julian edelman 13 yards (tackle robert alford)",  "legarrette blount middle 7 yards (tackle robert alford , ricardo allen)",  "legarrette blount middle 2 yards (tackle de'vondre campbell)",  "tom brady pass complete deep right julian edelman 27 yards (tackle robert alford)",  "legarrette blount left tackle 4 yards (tackle deion jones). legarrette blount fumbles (forced deion jones) recovered robert alford @ atl-29 (tackle julian edelman). penalty on martellus bennett: offensive holding (declined)",  "matt ryan pass complete short middle julio jones 19 yards (tackle logan ryan)",  "matt ryan pass complete deep left julio jones 23 yards",  "devonta freeman right tackle 15 yards (tackle devin mccourty)",  "devonta freeman left tackle 9 yards (tackle duron harmon)",  "timeout #1 new england patriots", "devonta freeman left end 5 yards touchdown",  "matt bryant kicks point good", "matthew bosher kicks off 69 yards returned dion lewis 20 yards (tackle sharrod neasman)",  "tom brady pass incomplete deep right intended malcolm mitchell (defended c.j. goodwin)",  "dion lewis middle 3 yards (tackle ricardo allen)", "tom brady pass complete short left james white 5 yards (tackle c.j. goodwin)",  "ryan allen punts 38 yards", "matt ryan pass complete deep right taylor gabriel 24 yards (tackle devin mccourty)",  "matt ryan pass complete deep left julio jones 18 yards (tackle eric rowe)",  "tevin coleman right tackle 1 yard (tackle alan branch)",  "matt ryan pass incomplete deep right intended austin hooper (defended patrick chung)",  "matt ryan pass complete deep left austin hooper 19 yards touchdown. penalty on patrick chung: defensive pass interference (declined)",  "penalty on shea mcclellin: illegal formation 5 yards (no play)",  "matt bryant kicks point good", "matthew bosher kicks off 65 yards touchback",  "tom brady pass complete short middle martellus bennett 12 yards (tackle jalen collins , keanu neal)",  "tom brady pass incomplete short middle intended julian edelman",  "tom brady pass incomplete deep middle intended danny amendola",  "tom brady pass complete short right james white 8 yards (tackle deion jones , de'vondre campbell). penalty on robert alford: defensive holding 5 yards (no play)",  "legarrette blount left tackle no gain (tackle joe vellano , jonathan babineaux)",  "james white right end 7 yards (tackle robert alford)",  "tom brady pass incomplete deep right intended julian edelman. penalty on brian poole: defensive holding 5 yards (no play)",  "legarrette blount right end 1 yard (tackle tyson jackson)",  "tom brady pass incomplete short right intended dion lewis",  "timeout #1 atlanta falcons", "tom brady pass incomplete short middle intended julian edelman. penalty on brian poole: defensive holding 5 yards (no play)",  "legarrette blount middle no gain (tackle ra'shede hageman)",  "tom brady pass complete short middle martellus bennett 13 yards (tackle ayodeji olatoye)",  "dion lewis right guard 4 yards (tackle de'vondre campbell)",  "dion lewis left guard no gain (tackle grady jarrett)",  "tom brady pass incomplete short left intended danny amendola intercepted robert alford @ atl-18 , returned 82 yards touchdown",  "matt bryant kicks point good", "matthew bosher kicks off 65 yards touchback",  "james white right guard 3 yards (tackle ra'shede hageman , robert alford)",  "tom brady pass complete short left martellus bennett 15 yards (tackle keanu neal , ricardo allen)",  "timeout #2 atlanta falcons", "tom brady pass incomplete deep right intended julian edelman",  "tom brady pass complete short right james white 28 yards (tackle ricardo allen)",  "tom brady pass complete short right chris hogan 8 yards (tackle keanu neal)",  "tom brady pass incomplete short right intended chris hogan",  "tom brady pass complete short middle james white 6 yards (tackle deion jones)",  "timeout #2 new england patriots", "tom brady pass incomplete short left intended julian edelman (defended robert alford)",  "tom brady pass complete short left james white 5 yards (tackle keanu neal). penalty on martellus bennett: offensive holding 10 yards",  "tom brady pass complete short left martellus bennett -3 yards (tackle ayodeji olatoye , grady jarrett)",  "stephen gostkowski 41 yard field goal good", "timeout #3 new england patriots",  "stephen gostkowski kicks off 38 yards", "stephen gostkowski kicks off 60 yards returned eric weems 14 yards (tackle nate ebner)",  "devonta freeman left tackle -3 yards (tackle dont'a hightower)",  "matt ryan pass complete short middle devonta freeman 7 yards (tackle logan ryan)",  "matt ryan pass incomplete short left intended taylor gabriel (defended eric rowe)",  "matthew bosher punts 56 yards returned julian edelman 26 yards (tackle c.j. goodwin)",  "atl challenged runner in bounds ruling , play overturned. matthew bosher punts 56 yards returned julian edelman 26 yards (tackle eric weems)",  "tom brady pass incomplete deep left intended chris hogan",  "tom brady pass complete short right danny amendola -2 yards (tackle brian poole). penalty on chris hogan: offensive pass interference (declined)",  "timeout #1 atlanta falcons", "tom brady pass incomplete short middle intended julian edelman",  "ryan allen punts 40 yards fair catch eric weems", "matt ryan pass complete short middle taylor gabriel 17 yards (tackle eric rowe)",  "tevin coleman right tackle 5 yards (tackle elandon roberts)",  "matt ryan pass complete deep middle taylor gabriel 35 yards (tackle duron harmon)",  "tevin coleman left end no gain (tackle patrick chung)",  "matt ryan pass complete short middle mohamed sanu 13 yards (tackle eric rowe)",  "devonta freeman right tackle 9 yards (tackle trey flowers)",  "devonta freeman middle -3 yards (tackle eric rowe , rob ninkovich)",  "matt ryan pass incomplete short middle intended taylor gabriel (defended malcolm butler). penalty on malcolm butler: defensive pass interference 3 yards (no play)",  "matt ryan pass complete short right tevin coleman 6 yards touchdown",  "matt bryant kicks point good", "matthew bosher kicks off 65 yards touchback",  "tom brady pass complete short middle dion lewis 2 yards (tackle keanu neal)",  "tom brady pass complete short left james white 12 yards (tackle jalen collins , ricardo allen)",  "dion lewis middle 8 yards (tackle jalen collins)", "dion lewis middle -1 yards (tackle brooks reed)",  "julian edelman pass incomplete deep right intended dion lewis",  "tom brady pass complete short left danny amendola 17 yards (tackle de'vondre campbell)",  "tom brady pass complete short left danny amendola 2 yards (tackle jalen collins)",  "tom brady pass incomplete short left intended julian edelman",  "tom brady middle 15 yards (tackle robert alford)", "legarrette blount right guard 4 yards (tackle keanu neal)",  "legarrette blount right tackle 9 yards (tackle robert alford)",  "legarrette blount middle 2 yards (tackle brooks reed)",  "tom brady pass complete short left james white 5 yards touchdown",  "stephen gostkowski kicks point no good", "stephen gostkowski kicks onside 11 yards recovered laroy reynolds. penalty on stephen gostkowski: illegal touch kick 5 yards",  "matt ryan pass complete short left austin hooper 9 yards (tackle duron harmon)",  "tevin coleman left tackle -1 yards (tackle trey flowers). penalty on jake matthews: offensive holding 10 yards (no play)",  "timeout #2 atlanta falcons", "matt ryan pass incomplete short right intended austin hooper (defended patrick chung)",  "--", "penalty on matthew bosher: delay of game 5 yards (no play)",  "matthew bosher punts 42 yards returned patrick chung -1 yards (tackle justin hardy)",  "tom brady pass complete deep right malcolm mitchell 15 yards (tackle jalen collins)",  "tom brady pass complete short middle malcolm mitchell 7 yards (tackle jalen collins)",  "james white middle 6 yards (tackle keanu neal)", "tom brady pass incomplete deep left intended julian edelman",  "tom brady pass complete short right malcolm mitchell 18 yards (tackle robert alford)",  "tom brady pass complete short right james white 9 yards (tackle robert alford)",  "tom brady pass incomplete short middle intended danny amendola",  "tom brady pass complete deep right martellus bennett 25 yards (tackle keanu neal)",  "tom brady sacked grady jarrett -5 yards", "tom brady pass complete short left james white 2 yards (tackle jalen collins , de'vondre campbell)",  "tom brady sacked grady jarrett -5 yards", "stephen gostkowski 33 yard field goal good",  "stephen gostkowski kicks off 48 yards returned justin hardy 10 yards (tackle barkevious mingo)",  "tevin coleman right end 8 yards (tackle patrick chung)",  "tevin coleman middle 1 yard (tackle trey flowers , logan ryan)",  "matt ryan sacked dont'a hightower -11 yards. matt ryan fumbles (forced dont'a hightower) recovered alan branch @ atl-25 (tackle chris chester)",  "tom brady sacked dwight freeney -5 yards", "tom brady pass complete short middle james white 4 yards (tackle keanu neal)",  "tom brady pass complete short left malcolm mitchell 12 yards (tackle c.j. goodwin)",  "tom brady pass complete short left danny amendola 8 yards (tackle ricardo allen)",  "tom brady pass complete short left danny amendola 6 yards touchdown",  "two point attempt: james white middle conversion succeeds",  "stephen gostkowski kicks off 62 yards returned justin hardy 7 yards (tackle jonathan jones)",  "matt ryan pass complete short left devonta freeman 39 yards (tackle elandon roberts)",  "devonta freeman right end 2 yards (tackle jabaal sheard , patrick chung)",  "matt ryan pass complete deep right julio jones 27 yards",  "devonta freeman left end -1 yards (tackle devin mccourty)",  "matt ryan sacked trey flowers -12 yards", "timeout #1 new england patriots",  "matt ryan pass complete short left mohamed sanu 9 yards (tackle logan ryan). penalty on jake matthews: offensive holding 10 yards (no play)",  "matt ryan pass incomplete short left intended taylor gabriel",  "matthew bosher punts 36 yards fair catch julian edelman",  "tom brady pass incomplete short right intended james white",  "tom brady pass incomplete deep right intended chris hogan",  "tom brady pass complete short right chris hogan 16 yards (tackle jalen collins)",  "tom brady pass incomplete short middle intended julian edelman (defended robert alford)",  "tom brady pass complete short left malcolm mitchell 11 yards (tackle jalen collins)",  "tom brady pass complete deep middle julian edelman 23 yards (tackle keanu neal)",  "atl challenged pass completion ruling , play upheld.",  "tom brady pass complete deep right danny amendola 20 yards (tackle brian poole)",  "tom brady pass complete short right james white 13 yards (tackle brian poole , ricardo allen)",  "tom brady pass complete short right james white 7 yards (tackle deion jones)",  "james white right guard 1 yard touchdown", "two point attempt: tom brady pass complete danny amendola conversion succeeds. penalty on dwight freeney: defensive offside 5 yards",  "stephen gostkowski kicks off 60 yards returned eric weems 11 yards (tackle brandon bolden)",  "matt ryan pass complete short left mohamed sanu 12 yards (tackle logan ryan)",  "matt ryan pass complete short right austin hooper 4 yards (tackle malcolm butler)",  "matt ryan spiked ball", "matt ryan pass incomplete deep left intended austin hooper",  "matthew bosher punts 38 yards fair catch julian edelman",  "dion lewis 13 yards", "matthew bosher kicks off 65 yards touchback",  "tom brady pass complete short left james white 6 yards (tackle deion jones)",  "tom brady pass complete short right danny amendola 14 yards",  "tom brady pass complete short left chris hogan 18 yards (tackle keanu neal , deion jones)",  "tom brady pass complete short left james white -3 yards (tackle deion jones)",  "tom brady pass complete short left julian edelman 15 yards (tackle robert alford)",  "james white right end 10 yards (tackle robert alford)",  "tom brady pass incomplete short right intended martellus bennett (defended de'vondre campbell). penalty on de'vondre campbell: defensive pass interference 13 yards (no play)",  "tom brady pass incomplete short right intended martellus bennett (defended vic beasley)",  "james white right end 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 deion jones) recovered robert alford @ 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 , 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 martellus bennett -grady jarrett)",  na, na, na, "nate ebner)", "devonta freeman left-dont'a hightower)",  "logan ryan)", na, "c.j. goodwin)", "atl challenged runner in bounds ruling , play overturned. eric weems)",  na, "tom brady pass complete short right danny amendola -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 -rob ninkovich)",  na, na, na, na, "keanu neal)", "ricardo allen)", "jalen collins)",  "dion lewis middle -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 patrick chung -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 dont'a hightower -11 yards. matt ryan fumbles (forced dont'a hightower) recovered alan branch @ atl-chris chester)",  na, "keanu neal)", "c.j. goodwin)", "ricardo allen)", na, na,  "jonathan jones)", "elandon roberts)", "patrick chung)", na,  "devonta freeman left end -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 james white -deion jones)",  "robert alford)", "robert alford)", na, na, na)), .names = c("detail",  "is.tackle", "tackler"), class = "data.frame", row.names = c(na,  -189l)) 

)

thanks 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 julian edelman 5 yards (tackle c.j. goodwin) 42          tom brady pass complete short left james white 5 yards (tackle c.j. goodwin) 89  matthew bosher punts 56 yards returned julian edelman 26 yards (tackle c.j. goodwin) 147   tom brady pass complete short left malcolm mitchell 12 yards (tackle c.j. goodwin)     is.tackle tackler 10       true    <na> 42       true    <na> 89       true    <na> 147      true    <na> 

here quick solution using stringr package.

plays<-c("matthew bosher kicks off 65 yards touchback",           "tom brady pass incomplete short middle intended julian edelman",           "tom brady pass complete short right julian edelman 9 yards (tackle philip wheeler)",           "legarrette blount right tackle no gain (tackle deion jones)")  library(stringr) #find lines pattern "(tackles....)" identified str<-str_extract(plays, "\\(tackle.+?\\)") #find last 2 words before closing ) tacklers<-str_extract(str, "\\w+ \\w+\\)") #replace ) blank tacklers<-str_replace(tacklers, "\\)", "") 

the stringr package has more consistent interface grep , gsub commands

edit: if play made more 1 player or initials involve per comment try:

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

i had add ? string extract statement in order make regex statement non-greedy in avoid plays penalty involved. there still 1 potential problem line 31 first of 2 tackles retrieved.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -