R語言報錯:Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : n行沒有x元素
阿新 • • 發佈:2020-08-29
今天在使用R語言讀取一個txt檔案時,報如下錯誤。
> data <- read.table("D:\\StudentTest\\評分.txt", sep="\t",header=T, fileEncoding="CP936") Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, : 9行沒有2元素
在調整各個引數後,任然還是該錯誤,於是懷疑是否是資料檔案的問題,於是,使用notepad++開啟檔案後可以看到,問題到底是出在哪裡?
由圖片可以知道,最後兩行的資料,並不是NA,還是真實的沒有,因此會報錯。現在只需要手動把這兩行調整一下就行。
現在資料就OK了,可以成功讀取。
> data <- read.table("D:\\PraviteFiles\\工作\\出版社\\R語言程式設計辭典大全\\程式碼\\R語言例項檔案_全部\\chap08\\sec02\\StudentTest\\評分.txt", sep="\t",header=T, fileEncoding="CP936") > data 本店 對手店 1 70 80 2 75 75 3 70 80 4 85 85 5 90 85 6 70 90 7 80 75 8 75 90 9 75 NA10 85 NA
轉載請註明:
微信公眾號:資料志(原:跟著菜鳥一起學R語言)
簡書:資料志