Difference Between read.table & read.delim Functions in R (3 Examples)
This tutorial helps to understand the difference between the read.table() and read.delim() functions in R programming.
In order to read a .txt, .csv or .tsv file we can use two different functions: read.table() and read.delim(). But what is the difference between them? This will be explained in the following tutorial.
The table of content has this structure:
Let’s see what happens with these two functions.
Sample Data
For this tutorial, we will need to create some sample data. We will create a .txt file with columns delimited by tabs that looks like this:
Example 1: Using read.table() Function to Read a Tab-Separated File
The read.table() function allows us to read a file into a data frame. This file can be tab-delimited, comma-delimited or any other delimiter. Also, the first row will be used as the column names if the “header=” argument is set to TRUE.
Let’s use the read.table() function to read our file:
data_sample <- read.table('our_file.txt', header = TRUE) data_sample
Example 2: Using read.delim() to Read a Tab-Separated File
The read.delim() function is a general function for other functions like read.csv() or read.tsv(), which use “,” as a default separator between columns.
The default delimiter in read.delim() is tab ‘\t’, but this can be changed to other types of delimiters.
Let’s see how the read.delim() function reads our file:
data_sample <- read.delim('our_file.txt') data_sample
As we can see, we obtain the same output using both functions. But what if our file is delimited by a different character?
Example 3: Using read.table() and read.delim() to Read a Space-Separated File
Let’s say we have our file, but delimited with spaces:
Let’s see how the read.table() function reads our space-separated file:
data_sample <- read.table('our_file.txt', header = TRUE) data_sample
We obtain the same output using the read.table() function in comparison to a tab-separated file or a space-separated file. What about the read.delim() function?
data_sample <- ('our_file.txt') data_sample
It seems like the read.delim() function won’t read our file by columns without specifying that our column separator is now a space character.
As shown, the read.delim() function needs the “sep=” argument to be specified when our delimiter is different from the tab delimiter, and takes the first row as the column name by default.
Meanwhile, the read.table() function doesn’t need the “sep=” argument to be specified if our delimiter is the tab or the space character (but does need it for comma-separated values and other delimiters), and needs the argument “header=” to be specified.
Thus, the main difference between these two functions is that read.delim() is used mainly to read tab-delimited files, and the read.table() function is a more general function that also reads space-delimited files by default.
Video, Further Resources & Summary
Do you need more explanations on the difference between read.table & read.delim Functions in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some of the other tutorials on Statistics Globe:
- Difference Between data.frame & as.data.frame Functions in R
- R Error in read.table: more columns than column names
- Difference Between facet_grid & facet_wrap ggplot2 Functions in R
- Copy & Paste Data from Clipboard into R
This post shows the difference between the read.table() function and read.delim() function in R. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.