2015-10-24 211 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)