Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read.table while using '#' as delimiter does not work?

Tags:

r

read.table

I have a data file with the # sign as delimiter, that I would like to read with the read.file command.

First of all; it's a big data file and I don't want to change the delimiter because:

  1. the risk of using a different delimiter that already exists in the data (note: can be checked, but point 2 makes this a little bit more complicated)
  2. I expect more of these data files with all the # sign as delimiter, so I don't want to change the data files every time when I would like to read a these files again

So I assumed I could use the sep argument of the read.file command. But it didn't worked out for the # sign as I expected. Only the first column is read. I tried some different delimiters, all worked fine, except for the # sign. See below for some examples, including the # delimiter.

The file looks like:

H1#H2#H3
a#b#c
d#e#f

Code in R performed including results, for the same file where I changed the delimiter. For =, |, @ and $ it works fine, but not for #...

> read.table(file='test_data.dat', check.names=F, sep='=', header=T)
H1 H2 H3
1  a  b  c
2  d  e  f
> read.table(file='test_data.dat', check.names=F, sep='|', header=T)
H1 H2 H3
1  a  b  c
2  d  e  f    
> read.table(file='test_data.dat', check.names=F, sep='@', header=T)
H1 H2 H3
1  a  b  c
2  d  e  f
> read.table(file='test_data.dat', check.names=F, sep='$', header=T)
H1 H2 H3
1  a  b  c
2  d  e  f
> read.table(file='test_data.dat', check.names=F, sep='#', header=T)
H1
1  a
2  d

Could anybody help me on this? Is this a known 'bug'? Is there a workaround?

Thanks in advance for the help!

like image 396
FBE Avatar asked Feb 21 '23 02:02

FBE


1 Answers

The comment character is also #, so you need something like:

read.table(file='tmp.txt', check.names=FALSE, sep='#', 
          header=TRUE, comment.char="@")
like image 88
csgillespie Avatar answered Feb 28 '23 15:02

csgillespie