Reputation: 511
I need to read a table that is a .tsv
file in R.
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?
Upvotes: 41
Views: 168168
Reputation: 51
You need to include fill = TRUE.
test <- read.table(file='drug_info.tsv', sep = '\t', header = TRUE, fill = TRUE)
Upvotes: 5
Reputation: 152
You can treat the data like a csv, and specify tab delimination.
read.csv("drug_info.tsv", sep = "\t")
Upvotes: 14
Reputation: 1821
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')
Upvotes: 6
Reputation: 771
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"))
Upvotes: 16
Reputation: 47591
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)
Upvotes: 5
Reputation: 2231
This should do it:
read.table(file = 'drug_info.tsv', sep = '\t', header = TRUE)
Upvotes: 47