我通過使用Lending Club的歷史貸款數據集來學習R。數據的代表性子集在這裏:https://gist.github.com/adetch/11b1c2b6eac0b6add23f將某些colClasses設置爲整數時,read.table在發現引用字段值時發生抱怨
的命令問題:
problem <- read.csv("test.csv",na.strings=c("","<NA>"),colClasses=c("mths_since_last_major_derog"="integer"))
我打的錯誤:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
scan() expected 'an integer', got '""'
我打了一個類似的問題,用下面的命令:
problem <- read.csv("test.csv",na.strings=c("","<NA>"),colClasses=c("id"="integer"))
這種情況下的錯誤:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
scan() expected 'an integer', got '"1077501"'
所以它似乎/在我看來,
R
的integer
類是用引號- 和不兼容的可能,該
na.strings
轉換類的掃描和integer
後運行檢查空字符串失敗。
然而,其他列包裹在""
如member_id
和loan_amnt
得到鑄成integer
無投訴(同時也沒有使用任何colClasses
特殊干預!)。
貼近的問題:
- 我怎麼投這些領域(
id
,mths_since_last_major_derog
)爲整數,而不是因子(注意有一些應該澆鑄成的因素衆多領域)
更重要的是:
- 哪裏是我的心智模式類,類的強制,read.table/read.csv等分解?
我從來沒有使用名爲向量colClasses前,但我確實看到可能非常有用。對'scan'進行了一些更改以適應R 3.0中的長整數/數字處理更改。也許這是一個副作用? –