Read.table when using '#' as a separator does not work?

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

Primarily; it is a large data file, and I do not want to change the delimiter because:

  • the risk of using another delimiter that already exists in the data (note: you can check, but point 2 makes it a little more complicated)
  • I expect more of these data files with all # characters as a separator, so I don't want to change the data files every time I would like to read these files again.

So, I assumed that I could use the sep argument of the read.file . But this did not work for the # sign, as I expected. Only the first column has been read. I tried different delimiters, everything worked fine except for the # sign. Below are some examples, including the # separator.

The file looks like this:

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

Code in R containing the 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 abc 2 def > read.table(file='test_data.dat', check.names=F, sep='|', header=T) H1 H2 H3 1 abc 2 def > read.table(file='test_data.dat', check.names=F, sep='@', header=T) H1 H2 H3 1 abc 2 def > read.table(file='test_data.dat', check.names=F, sep='$', header=T) H1 H2 H3 1 abc 2 def > read.table(file='test_data.dat', check.names=F, sep='#', header=T) H1 1 a 2 d 

Can someone help me with this? Is this a known bug? Is there a workaround?

Thanks in advance for your help!

+4
source share
1 answer

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

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

Source: https://habr.com/ru/post/1402460/


All Articles