title: Convert one row to multiple rows per subject in a data frame by splitting text data in R
body: <p>I have a dataset with a patient identifier and a text field with a summary of medical findings (1 row per patient).  I would like to create a dataset with multiple rows per patients by splitting the text field so that each sentence of the summary falls on a different line.  Subsequently, I would like to text parse each line looking for certain keywords and negation terms.  An example of the structure of the data frame is (the letters represent the sentences):</p>
  
  <p><strong>ID       Summary</strong><br>
  1   aaaaa. bb. c<br>
  2       d. eee. ff. g. h<br>
  3       i. j<br>
  4       k  </p>
  
  <p>I would like to split the text field at the “.” to convert it to:  </p>
  
  <p><strong>ID        Summary</strong><br>
  1       aaaaa<br>
  1       bb<br>
  1       c<br>
  2       d<br>
  2       eee<br>
  2       ff<br>
  2       g<br>
  2       h<br>
  3       i<br>
  3       j<br>
  4       k  </p>
  
  <p>R code to create the initial data frame:  </p>
  
  <pre><code>ID &lt;- c(1, 2, 3, 4)  
  Summary &lt;- c("aaaaa. bb. c", "d. eee. ff. g. h", "i. j", "k")  
  
  df &lt;- data.frame(cbind(ID, Summary))  
  df$ID &lt;- as.numeric(df$ID)  
  df$Summary &lt;- as.character(df$Summary)  
  </code></pre>
  
  <p>The following previous posting provides a nice solution:
  <a href="https://stackoverflow.com/questions/16668699/breaking-up-melting-text-data-in-a-column-in-r">Breaking up (melting) text data in a column in R?</a>  </p>
  
  <p>I used the following code from that posting which works for this sample dataset:  </p>
  
  <pre><code>dflong &lt;- by(df, df$ID, FUN = function(x) {  
    sentence = unlist(strsplit(x$Summary, "[.]"))  
    data.frame(ID = x$ID, Summary = sentence)  
    })  
  dflong2&lt;- do.call(rbind,dflong)  
  </code></pre>
  
  <p>However, when I try to apply to my larger dataset (>200,000 rows), I get the error message:<br>
  Error in data.frame(ID = x$ID, Summary = sentence) :  arguments imply differing number of rows: 1, 0</p>
  
  <p>I reduced the data frame down to test it on a smaller dataset and I still get this error message any time the number of rows is >57.</p>
  
  <p>Is there another approach to take that can handle a larger number of rows?  Any advice is appreciated.  Thank you.</p>