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

纵饮孤独 提交于 2019-12-01 04:49:23

问题


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!


回答1:


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

read.table(file='tmp.txt', check.names=FALSE, sep='#', 
          header=TRUE, comment.char="@")


来源:https://stackoverflow.com/questions/9789282/read-table-while-using-as-delimiter-does-not-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!