title: Importing txt file in R --- Why does # symbol in variable names cause problems?
body: <p>I am trying to import some <code>.txt</code> data into <code>R</code> using the <code>read.table</code> function.  The actual dataset I have is large and complicated, so I will use a small reproducible example here which shows the kind of problem I'm having.  To illustrate the problem, consider the following two textual files:</p>
  <pre><code>TEST.txt
  |VAR1  |VAR2  |
  |12    |F     |
  |56    |B     |
  |18    |A     |
  
  TEST2.txt
  |VAR1  |VAR2# |
  |12    |F     |
  |56    |B     |
  |18    |A     |
  </code></pre>
  <p>(I am aware that these have more separator characters than is needed to get the two columns of data; this is how my textual data is constructed.  This is easy for me to deal with by ignoring the first and last variables in the imported data frame, but it seems to affect the problem I'm experiencing so I have included it in the reproducible example.)  This is what happens when I try to import each of these data frames:</p>
  <pre><code>#Import the first table (this works fine)
  read.table('TEST.txt', header = TRUE, fill = FALSE, sep = '|', strip.white = TRUE)[, 2:3]
  
    VAR1 VAR2
  1   12    F
  2   56    B
  3   18    A
  
  #Import the second table (doesn't work)
  read.table('TEST2.txt', header = TRUE, fill = FALSE, sep = '|', strip.white = TRUE)[, 2:3]
  
  Error in read.table(&quot;TEST2.txt&quot;, header = TRUE, fill = FALSE, sep = &quot;|&quot;,  : 
    duplicate 'row.names' are not allowed
  </code></pre>
  <p>As you can see, the first data frame (without the hash symbol) imports correctly but the second one (with the has symbol) doesn't import properly.  Obviously the hash symbol in the variable name is causing a problem, but I'm not sure why, or how to deal with this.</p>
  <hr />
  <p><strong>Question:</strong> Why is this problem occurring?  What is the simplest way to fix the problem so that the latter data frame imports correctly.  (I would prefer not to make any change to the underlying textual file, so would prefer a solution using the <code>R</code> commands.)</p>