title: Concatenating rows of strings in R based on what is in the subsequent row
body: <p>I am hoping that someone can help me with this. I am trying to create a corpus of texts. I have scanned the texts in and OCRed them, cleaned the text, and imported the text into a data frame in R.</p>
  <p>Because of the way the scanning process worked, there is a carriage return at the end of each line, and a blank line after each paragraph.</p>
  <p>I am wondering if there is an easy way to combine all of the rows from the same paragraph into one row. In other words, concatenate the rows if there is text in both rows, but not if it is &quot;&quot; or NA (either would be fine).</p>
  <p>Here is an example of what I have (the actual df is much larger):</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th></th>
  <th>chpt</th>
  <th>page</th>
  <th>text</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>Equations and formulae</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  <td></td>
  </tr>
  <tr>
  <td>3</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>Equations, identities and formulae</td>
  </tr>
  <tr>
  <td>4</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  <td></td>
  </tr>
  <tr>
  <td>5</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>You will encounter a wide variety of equations in this course. Essentially, an</td>
  </tr>
  <tr>
  <td>6</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>equation is a statement equating two algebraic expressions that may be true or</td>
  </tr>
  <tr>
  <td>7</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>false depending upon the value(s) substituted for the variable(s). Values of the</td>
  </tr>
  <tr>
  <td>8</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>variables that make the equation true are called solutions or roots of the equation.</td>
  </tr>
  <tr>
  <td>9</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>All of the solutions to an equation comprise the solution set of the equation.</td>
  </tr>
  <tr>
  <td>10</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  <td></td>
  </tr>
  <tr>
  <td>11</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>An equation that is true for all possible values of the variable is called an identity.</td>
  </tr>
  <tr>
  <td>12</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  <td></td>
  </tr>
  <tr>
  <td>13</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>- different methods to solve a system of linear equations (maximum of</td>
  </tr>
  <tr>
  <td>14</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>three equations in three unknowns)</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>What I would like:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th></th>
  <th>chpt</th>
  <th>page</th>
  <th>text</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>Equations and formulae</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  </tr>
  <tr>
  <td>3</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>Equations, identities and formulae</td>
  </tr>
  <tr>
  <td>4</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  </tr>
  <tr>
  <td>5</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>You will encounter a wide variety of equations in this course. Essentially, an equation is a statement equating two algebraic expressions that may be true or false depending upon the value(s) substituted for the variable(s). Values of the variables that make the equation true are called solutions or roots of the equation. All of the solutions to an equation comprise the solution set of the equation.</td>
  </tr>
  <tr>
  <td>10</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  </tr>
  <tr>
  <td>11</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td>An equation that is true for all possible values of the variable is called an identity.</td>
  </tr>
  <tr>
  <td>12</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td></td>
  </tr>
  <tr>
  <td>13</td>
  <td>Chpt 01</td>
  <td>2</td>
  <td> - different methods to solve a system of linear equations (maximum of three equations in three unknowns)</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>The example df itself looks like this:</p>
  <pre><code>structure(list(chpt = c(&quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, 
  &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, 
  &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;, &quot;Chpt 01&quot;), page = c(2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), text = c(&quot;Equations and formulae&quot;, 
  &quot;&quot;, &quot;Equations, identities and formulae&quot;, &quot;&quot;, &quot;You will encounter a wide variety of equations in this course. Essentially, an&quot;, 
  &quot;equation is a statement equating two algebraic expressions that may be true or&quot;, 
  &quot;false depending upon the value(s) substituted for the variable(s). Values of the&quot;, 
  &quot;variables that make the equation true are called solutions or roots of the equation.&quot;, 
  &quot;All of the solutions to an equation comprise the solution set of the equation.&quot;, 
  &quot;&quot;, &quot;An equation that is true for all possible values of the variable is called an identity.&quot;, 
  &quot;&quot;, &quot;- different methods to solve a system of linear equations (maximum of&quot;, 
  &quot;three equations in three unknowns)&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -14L))
  </code></pre>