Read csv data file in R

烂漫一生 提交于 2019-12-02 07:35:27

The error you get is caused by a the colClasses argument - some values in the file to not match the datatypes you specified.

Most of the time I encounter something like this, I probably just had some counting problem with the colClasses argument, e.g it would maybe be

colClasses=c(rep("factor",5),"numeric", rep("factor",4), rep("numeric",7),"factor")

instead of your default values. That may be simply checked by carefully comparing the contents of the first lines of your file with the datatypes you specified.

If this does not do the trick for you, you probably have some wrong datatype where you do not expect it. A simple, yet slow approach is to remove the colClasses argument and first read the whole file without specific options - probably add stringsAsFactors=FALSE to get only character values. This probably should work.

Then you may try to convert each column one by one, like

data$itemId <- as.numeric(data$itemId)

and then check the result for NA values, easily done by summary(data$itemId). If you got NA values, you can call which(is.na(data$itemId)) to get the row number and check your original file whether the NA in fact is valid or if you have some data problems there.

Most of the time you will be able to narrow down your problem this way.

If your file a lot of columns, however, this quickly becomes a lot of work....

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!