title: Need to extract individual characters from a string column using R
body: <p><strong>Background</strong></p>
  <p>Below is my <em>gamedata</em> dataset in dput form -- it contains line scores for some MLB games.</p>
  <pre><code>structure(list(team = c(&quot;NYM&quot;, &quot;NYM&quot;, &quot;BOS&quot;, &quot;NYM&quot;, &quot;BOS&quot;), linescore = c(&quot;010000000&quot;, 
  &quot;(10)1140006x&quot;, &quot;002200010&quot;, &quot;00000(11)01x&quot;, &quot;311200&quot;), ondate = structure(c(18475, 
  18476, 18487, 18489, 18494), class = &quot;Date&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -5L))
  </code></pre>
  <p>For example, here is one line score: &quot;002200010&quot;.</p>
  <p>Some line scores end with an 'x' and some have a two-digit value in parentheses, as in &quot;00000(11)01x&quot;. Each digit not in parentheses indicates how many runs the team scored in the inning. If a team scores more than nine runs in an inning, the number is placed in parentheses, so in the line score &quot;00000(11)01x&quot; the team scored 11 runs in the sixth inning and did not come to bat in the bottom of the ninth (shown by the 'x').</p>
  <p>Not every line score has nine innings. Some have more and some have as few as six.</p>
  <p><strong>What I need to do</strong></p>
  <p><strong>First,</strong> what I need to do is to get how many runs a team scored in each inning, e.g., the first, second, third, and so on, and put each <em>runs scored</em> in a new column. I would prefer for the solution to use dplyr.</p>
  <p>I have reviewed stackoverflow's suggested solutions, but found none that matched what I need. If there is one, I would appreciate it if you would share its URL.</p>
  <p>I have tried to do it using this code:</p>
  <pre><code>gamedata %&gt;%
    select(ondate, team, linescore) %&gt;%
    mutate(inng1 = str_extract(linescore, &quot;\\d|\\(\\d{2}\\)&quot;))
  </code></pre>
  <p><strong>Here is the output:</strong></p>
  <pre><code>ondate      team linescore    inng1
  2020-08-01  NYM 010000000       0   
  2020-08-02  NYM (10)1140006x  (10)  
  2020-08-13  BOS 002200010       0   
  2020-08-15  NYM 00000(11)01x    0   
  2020-08-20  BOS 311200          3
  </code></pre>
  <p><strong>Second,</strong> how can I remove the parentheses in the <em>inng1</em> column for '10'?</p>
  <p><strong>The code below produced the error beneath it</strong>:</p>
  <pre><code>gamedata %&gt;%
    select(ondate, team, linescore) %&gt;%
    mutate(inng1 = str_extract(linescore, &quot;\\d|\\(\\d{2}\\)&quot;))
   str_remove_all(inng1,&quot;[()]&quot;)
  </code></pre>
  <p>This is the error message I got:</p>
  <blockquote>
  <p>&quot;Error in stri_replace_all_regex(string, pattern, fix_replacement(replacement), : object 'inng1' not found&quot;</p>
  </blockquote>
  <p><strong>Third,</strong> I need to know how to extract the runs scored for each additional inning, starting with the second, putting each value in its own column, e.g, inng2, inng3, and so one.</p>
  <p>At the end, I should have the output shown above (without parentheses for each double-digit inning) with a column for each inning, so there would be a column titled &quot;inng1,&quot; &quot;inng2,&quot; inng3&quot;, &quot;inng4&quot;, and so on. The data in the inning columns needs to be numeric as later I will be summing them.</p>