How to import a .tsv file

I need to read a table that is a .tsv file in R.

enter image description here

test <- read.table(file='drug_info.tsv') # Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : # line 1 did not have 10 elements test <- read.table(file='drug_info.tsv', ) # Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : # line 1 did not have 10 elements scan("drug_info.tsv") # Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : # scan() expected 'a real', got 'ChallengeName' scan(file = "drug_info.tsv") # Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, : # scan() expected 'a real', got 'ChallengeName' 

How should I read it?

3

6 Answers

This should do it:

read.table(file = 'drug_info.tsv', sep = '\t', header = TRUE) 
2

Using fread from the package data.table will read the data and will skip the error you are getting using read.table.

require(data.table) data<-as.data.frame(fread("drug_info.tsv")) 
1

You can treat the data like a csv, and specify tab delimination.

read.csv("drug_info.tsv", sep = "\t") 

Assuming that only the first line does not have the right number of elements, and that this is the column names line. Skip the first line:

 d <- read.table('drug_info.tsv', skip=1) 

Now read it

 first <- readLines('drug_info.tsv', n=1) 

Inspect it, fix it such that its number of elements matches d and then

 colnames(d) <- first 

If that does not work, you can do

 x <- readLines('drug_info.tsv') 

and diagnostics like this:

 sapply(x, length) 

You need to include fill = TRUE.

test <- read.table(file='drug_info.tsv', sep = '\t', header = TRUE, fill = TRUE) 

utils::read.delim() is most commonly used in such case if you don't want to install other library. The sample code could be something like:

test <- read.delim(file='drug_info.tsv') 

or much more friendly io functions could be available from readr library, where a read_tsv named function is available directly:

test <- readr::read_tsv('drug_info.tsv') 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like