2015-10-24 210 views
5

我需要阅读的表如何在R中导入.tsv文件?

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' 

我应该如何看呢?

+1

请复制/粘贴文件的前5行到您的问题,并删除图片。 –

+2

使用缺省设置 – rawr

+1

'read.table'默认使用空格分隔符(通常表示空格或制表符)。如果你有空格,你可以使用'sep =“\ t”'明确地将分隔符设置为tab。 'read.table'与有效的输入文件一起工作,所以如果在导入数据时出现问题,它与文件相关,而不是函数。因此,为了帮助您,我们需要您分享您实际尝试导入的文件样本,而不是其他程序中的数据图片。 – MrFlick

回答

9

这应做到:

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

这应该会给出与报告相同的错误,第1行没有足够的元素 – RobertH

+1

我认为这里的回调过早有点过早,因为我们没有任何实际的数据可以用任何方法测试。 –

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)