title: Reading files which contain unquoted newline characters in text fields using R
body: <p>I am trying to read a large table into R but one of the text fields occasionally contains one or more unquoted, un-escaped newline characters (<code>\n</code>), thus the <code>read.table()</code> function is not able to easily import this file. The file is pipe delimited and the text fields are not quoted.</p>
  
  <p>I can read it in if I pass the argument <code>fill=T</code> with <code>read.table()</code> but, of course, rows with newline characters in a text field are be corrupted by this.</p>
  
  <p>I have successfully been able to use <code>f &lt;- readChar(fname, nchars=file.info(fname)["size"], TRUE)</code> to read sub-segments of the file, then use <code>gsub()</code> to search and destroy the offending newline characters. (see code below) However, the full file is > 100mb, so <code>gsub()</code> does little more than turn my laptop into a hand-warmer (it's still trying to gsub all the newline characters as I write this). </p>
  
  <p>Anyone have any suggestions for how to efficiently read in a file like this?</p>
  
  <p>It seems like there should be some way of telling R to expect a certain number of delimiters before expecting a newline, but I haven't been able to find any way to do this in the documentation.</p>
  
  <p>Sorry, this seems like it should be easy, but it's really been stumping me, and I have not been able to find anything in stackoverflow or google offering a solution. </p>
  
  <p>Here is the code I've tried so far:</p>
  
  <p>attempt 1:</p>
  
  <pre><code>fdat = read.table(file=fname, 
              allowEscapes=F,
              stringsAsFactors=F,
              quote="", 
              fill=T,
              strip.white=T,
              comment.char="",
              header=T, 
              sep="|")
  </code></pre>
  
  <p>attempt 2:  </p>
  
  <pre><code>f &lt;- readChar(fname, nchars=file.info(fname)["size"], TRUE)
  f2 = gsub(pattern="\n(?!NCT)",replacement=" ",x=f, perl=T)
  fdat = read.table(text=f2,
              allowEscapes=F,
              stringsAsFactors=F,
              quote="", 
              fill=F,
              strip.white=T,
              comment.char="",
              header=T, 
              sep="|")
  </code></pre>
  
  <p>Here are a few lines from the file:</p>
  
  <pre><code>NCT_ID|DOWNLOAD_DATE|DOWNLOAD_DATE_DT|ORG_STUDY_ID|BRIEF_TITLE|OFFICIAL_TITLE|ACRONYM|SOURCE|HAS_DMC|OVERALL_STATUS|START_DATE|COMPLETION_DATE|COMPLETION_DATE_TYPE|PRIMARY_COMPLETION_DATE|PRIMARY_COMPLETION_DATE_TYPE|PHASE|STUDY_TYPE|STUDY_DESIGN|NUMBER_OF_ARMS|NUMBER_OF_GROUPS|ENROLLMENT_TYPE|ENROLLMENT|BIOSPEC_RETENTION|BIOSPEC_DESCR|GENDER|MINIMUM_AGE|MAXIMUM_AGE|HEALTHY_VOLUNTEERS|SAMPLING_METHOD|STUDY_POP|VERIFICATION_DATE|LASTCHANGED_DATE|FIRSTRECEIVED_DATE|IS_SECTION_801|IS_FDA_REGULATED|WHY_STOPPED|HAS_EXPANDED_ACCESS|FIRSTRECEIVED_RESULTS_DATE|URL|TARGET_DURATION|STUDY_RANK
  NCT00000105|Information obtained from ClinicalTrials.gov on September 25, 2012|9/25/2012|2002LS032|Vaccination With Tetanus and KLH to Assess Immune Responses.|Vaccination With Tetanus Toxoid and Keyhole Limpet Hemocyanin (KLH) to Assess Antigen-Specific Immune Responses||Masonic Cancer Center, University of Minnesota|Yes|Terminated|July 2002|March 2012|Actual|March 2012|Actual|N/A|Observational|Observational Model:  Case Control, Time Perspective:  Prospective||3|Actual|112|Samples With DNA|analysis of blood samples before and 4 weeks postvaccination|Both|18 Years|N/A|Accepts Healthy Volunteers|Probability Sample|-  Normal volunteers
  
            -  Patients with Cancer (breast, melanoma, hematologic)
  
            -  Transplant patients (umbilical cord blood transplant, autologous transplant)
  
            -  Patients receiving other cancer vaccines|March 2012|March 26, 2012|November 3, 1999|Yes|Yes|Replaced by another study.|No||http://clinicaltrials.gov/show/NCT00000105||6670
  NCT00000106|Information obtained from ClinicalTrials.gov on September 25, 2012|9/25/2012|NCRR-M01RR03186-9943|41.8 Degree Centigrade Whole Body Hyperthermia for the Treatment of Rheumatoid Diseases|||National Center for Research Resources (NCRR)||Active, not recruiting||||||N/A|Interventional|Allocation:  Randomized, Intervention Model:  Parallel Assignment, Primary Purpose:  Treatment|||||||Both|18 Years|65 Years|No|||November 2000|June 23, 2005|January 18, 2000||||No||http://clinicaltrials.gov/show/NCT00000106||7998
  </code></pre>
  
  <p>As can be seen, this sample lines from my problem file include the header (line #1), a problematic line (line #2), and a non-problematic line (line #3). Each non-header line starts with NCT and ends with \n (this was leveraged in gsub's regular expression). </p>
  
  <p>Any suggestions are much appreciated.</p>