How to Import TSV Files into R (Including Example)

Importing TSV files into R is a simple process. You will need to use the read.delim() function from the utils package in order to read in your TSV file. You can specify the delimiter within the function, and it will import the data as a data frame. As an example, if you have a TSV file called “data.tsv” stored in your working directory, you can import it into R using read.delim(“data.tsv”, sep=”t”). This will import the file into R with the specified delimiter and store it as a data frame.


You can use the following basic syntax to import a TSV file into R:

library(readr)

#import TSV file into data frame
df <- read_tsv('C:/Users/bob/Downloads/data.tsv')

The following examples show how to use this syntax in practice.

Example 1: Import TSV File into R (With Column Names)

Suppose I have the following TSV file called data.tsv saved somewhere on my computer:

I can use the following syntax to import this TSV file into a data frame in R:

library(readr)

#import TSV file into data frame
df <- read_tsv('C:/Users/bob/Downloads/data.tsv')

#view data frame
df

# A tibble: 5 x 3
  team  points rebounds
      
1 A         33       12
2 B         25        6
3 C         31        6
4 D         22       11
5 E         20        7

We can see that the TSV file was successfully imported into R.

Example 2: Import TSV File into R (No Column Names)

Suppose I have the following TSV file called data.tsv with no column names:

I can use the col_names argument to specify that there are no column names when importing this TSV file into R:

library(readr)

#import TSV file into data frame
df <- read_tsv('C:/Users/bob/Downloads/data.tsv', col_names=FALSE)

#view data frame
df

  X1       X2    X3
    
1 A        33    12
2 B        25     6
3 C        31     6
4 D        22    11
5 E        20     7

By default, R provides the column names X1, X2, and X3.

I can use the following syntax to easily :

#rename columns
names(df) <- c('team', 'points', 'rebounds')

#view updated data frame
df

  team  points rebounds
        
1 A         33       12
2 B         25        6
3 C         31        6
4 D         22       11
5 E         20        7

The following tutorials explain how to import other files in R:

x