Open In App

How to Import TSV Files into R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss how to import tsv files in R Programming Language

The TSV is an acronym for Tab Separated Values, in R these types of files can be imported using two methods one is by using functions present in readr package and another method is to import the tsv file by specifying the delimiter as tab space(‘\t’) in read.delim() function as follows.

Using readr Package to Import TSV Files into R

First, we need to install and load the readr package

R




# Install the required package
install.packages("readr")
# Load the installed Package
library(readr)


Next, using read_tsv() method present in readr package we are going to the content present in it. (The read_tsv method is going to return the contents in the file as data frame)

Syntax: read_tsv(file_path,col_names)

Where,

  •  file_path- path of the tsv file to be read
  • col_names – I False the column names are not displayed

R




# Reading the contents of TSV file using read_tsv() method
df<-readr::read_tsv("C:\\Users\\sri06\\Desktop\\Student_Details.tsv")
print(df)


Output:

 

Using read.delim() function to Import TSV Files into R

In general read.delim() function in R is used to read space separated files by default but by specifying the delimiter(separator) as ‘\t’  we can also read TSV files in R.

Syntax: read.delim(file_path.sep)

Where,

  • path_file – Path of the csv file to be read
  • sep – separator of the file(‘,’,’ ‘,’\t’) is specified here

R




# Read tsv files using read.delim() method
df<-read.delim("C:\\Users\\sri06\\Desktop\\Student_Details.tsv",sep="\t")
print(df)


Output:

 

Note:

By observing the obtained outputs the basic difference between both the methods is read_tsv() function returned the dataframe with columns by specifying the type of it [ Student_Id<dbl> – double, Student_Name<chr> – Character ], when it comes to read.delim() method it simply returns the data present in the tsv file.



Last Updated : 24 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads