32

我需要读取一个表,它是.tsvR 中的一个文件。

在此处输入图像描述

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'

我应该怎么读?

4

6 回答 6

42

这应该这样做:

read.table(file = 'drug_info.tsv', sep = '\t', header = TRUE)
于 2015-10-24T19:26:14.640 回答
12

使用包 data.table 中的 fread 将读取数据并跳过使用 read.table 时遇到的错误。

require(data.table)

data<-as.data.frame(fread("drug_info.tsv"))
于 2018-03-12T06:05:09.223 回答
10

您可以将数据视为 csv,并指定制表符分隔。

read.csv("drug_info.tsv", sep = "\t")
于 2019-02-21T22:39:23.810 回答
5

假设只有第一行没有正确数量的元素,并且这是列名行。跳过第一行:

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

现在读它

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

检查它,修复它,使其元素数量匹配d,然后

 colnames(d) <- first

如果这不起作用,你可以做

 x <- readLines('drug_info.tsv')  

和这样的诊断:

 sapply(x, length)
于 2015-10-24T19:52:04.913 回答
4

您需要包括 fill = TRUE。

test <- read.table(file='drug_info.tsv', sep = '\t', header = TRUE, fill = TRUE)
于 2019-11-15T20:58:08.047 回答
2

utils::read.delim()如果您不想安装其他库,则在这种情况下最常用。示例代码可能类似于:

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

或更友好的 io 函数可以从 获得readr library,其中read_tsv命名函数可以直接使用:

test <- readr::read_tsv('drug_info.tsv')
于 2019-01-26T15:43:58.890 回答