10

What is the difference between the read.table() and read.delim() functions in the R language?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
user1021713
  • 2,133
  • 8
  • 27
  • 40
  • 4
    You can type `?read.table` and `?read.delim` in to the R console to find out more about these functions (the help files for both are in the same place). That's probably what teucer did to pull up the help file he's quoting from. – Tyler Rinker May 15 '12 at 12:28

3 Answers3

26

In addition to reading help pages when you are unsure of what a function does, you can also examine the function's actual code. For example, entering read.delim reveals that the function contains the following code:

> read.delim
function (file, header = TRUE, sep = "\t", quote = "\"", dec = ".", 
    fill = TRUE, comment.char = "", ...) 
read.table(file = file, header = header, sep = sep, quote = quote, 
    dec = dec, fill = fill, comment.char = comment.char, ...)

Thus, read.delim() is simply a wrapper function for read.table() with default argument values that are convenient when reading in tab-separated data. It is exactly the same as calling:

read.table(file, header = TRUE, sep = "\t", quote = "\"", 
    dec = ".", fill = TRUE, comment.char = "")
jthetzel
  • 3,603
  • 3
  • 25
  • 38
4

From R help:

Similarly, read.delim and read.delim2 are for reading delimited files, defaulting to the TAB character for the delimiter. Notice that header = TRUE and fill = TRUE in these variants, and that the comment character is disabled.

teucer
  • 6,060
  • 2
  • 26
  • 36
0

read.delim() allows you to use custom delimiters ( ) within .txt files. For example, imagine you are running a textmining operation and you need to get the content of the document. You may want to add an additional year column to the contents of the documents. Let's say you wrote the year information before the content and you put a comma between it and separated it with the document content. If you then use "," as the delimiter when importing into R, it will conflict with the commas in the document content. Therefore, you may want to use a special character. After using this character, read.delim() helps you in importing to R as follows.

enter image description here]

tez<-read_delim(file.choose(),col_names = FALSE, delim = "\u255D")

Thus, you will get the 1.txt file in 3 columns.

NCC1701
  • 139
  • 11