Search code examples
rdataframeescaping

R - Avoid escaping characters when reading a table


I have the following table into a txt file (columns are separated by spaces) :

Symbol  ASCII_code  Q-score
 "      34          1
 #      35          2
 $      36          3
 %      37          4
 &      38          5
 '      39          6
 (      40          7
 )      41          8
 *      42          9
 +      43          10

I need to convert it into a dataframe in R, with 3 columns (1 character or number per case). I used the following command :

read.table(list_files, header = TRUE, sep = " ")

Unfortunately, R seems to be escaping the ", # and $, so that the result table begins at line 4 (% 37 4). Whatsmore, R takes the quote (') as a real quote, and reads all the following characters into a single string. Here is the result :

    Symbol                          ASCII_code  Q_score
1   %                               37          4
2   &                               38          5
3   39 6 ( 40 7 ) 41 8 * 42 ...     NA          NA

Does anyone has the solution to these problems ? I have many other special characters such as parentheses, slashes and @, so that I would need a solution that works for every escape characters, not only those 3.


Solution

  • You need to set quote = "" and comment.char = "#" in the read.table function.

    By default, these are set as quote = "\"'" and comment.char = "#". For more information have a look at ?read.table() documentation.

    below syntax usually works:

    read.table("path/to/you/file.txt", header = T,quote = "", comment.char = "")