Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using your data, modified to quote text fields that contain the separator (get whatever tool you used to generate the file to quote text fields for you!)</p> <pre><code>txt &lt;- "Temp Press Reagent 'Yield A' 'Conversion etc' degC bar /g % % 1 2 3 4 5 6 7 8 9 10 " </code></pre> <p>this snippet of code below reads the file in two steps</p> <ol> <li>First we read the data, so <code>skip = 2</code> means skip the first 2 lines</li> <li>Next we read the data again but only the first two line, this output is then further processed by <code>sapply()</code> where we <code>paste(x, collapse = " ")</code> the strings in the columns of the <code>labs</code> data frame. These are assigned to the <code>names</code> of <code>dat</code></li> </ol> <p>Here is the code:</p> <pre><code>dat &lt;- read.table(text = txt, skip = 2) labs &lt;- read.table(text = txt, nrows = 2, stringsAsFactors = FALSE) names(dat) &lt;- sapply(labs, paste, collapse = " ") dat names(dat) </code></pre> <p>The code, when runs produces:</p> <pre><code>&gt; dat &lt;- read.table(text = txt, skip = 2) &gt; labs &lt;- read.table(text = txt, nrows = 2, stringsAsFactors = FALSE) &gt; names(dat) &lt;- sapply(labs, paste, collapse = " ") &gt; &gt; dat Temp degC Press bar Reagent /g Yield A % Conversion etc % 1 1 2 3 4 5 2 6 7 8 9 10 &gt; names(dat) [1] "Temp degC" "Press bar" "Reagent /g" [4] "Yield A %" "Conversion etc %" </code></pre> <p>In your case, you'll want to modify the <code>read.table()</code> calls to point at the file on your file system, so use <code>file = "foo.txt"</code> in place of <code>text = txt</code> in the code chunk, where <code>"foo.txt"</code> is the name of your file.</p> <p>Also, if these headings don't start at the top of the file, then increase <code>skip</code> to <code>2+n</code> where <code>n</code> is the number of lines <strong>before</strong> the two header rows. You'll also need to add <code>skip = n</code> to the second <code>read.table()</code> call which generates <code>labs</code>, where <code>n</code> is again the number of lines before the header lines.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload