Home » How to Import TSV Files into R (Including Example)

How to Import TSV Files into R (Including Example)

by Tutor Aspire

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

library(readr)

#import TSV file into data frame
df 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 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 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 the columns:

#rename columns
names(df) 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

Note: You can find the complete documentation for the read_tsv() function here.

Additional Resources

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

How to Import CSV Files into R
How to Import a CSV from URL in R
How to Import Excel Files into R

You may also like