R中的read.csv中指定colClasses的问题 [英] problem with specifying colClasses in read.csv in R

查看:117
本文介绍了R中的read.csv中指定colClasses的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在read.csv中指定colClasses,以试图加快csv文件的读取速度.但是,我遇到以下问题:

I am trying to specify colClasses in read.csv in an attempt to speed up the reading of csv file. However, I encounter the following problem:

假设我有一个名为"t.csv"的文件:

assuming that i have a file called "t.csv":

"a","b"
"x","0"

然后,如果我在R中运行以下命令:

Then, if I run the following in R:

data <- read.csv('t.csv' , stringsAsFactors=FALSE, check.names=FALSE , comment.char='', colClasses= c('character','numeric') )

我收到此错误:

Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  scan() expected 'a real', got '"0"'

起初,我认为这是我的报价存在问题.但是在read.csv中使用quote =''并没有帮助.

At first I thought it was the problem with my quote. But using quote='"' in read.csv didn't help.

推荐答案

您的第二列不是被引用的numeric,而是文本.

Your second column is not numeric as it is quoted -- that makes it text.

因此,将其阅读为文本,然后在该列上调用as.numeric(...).或更改文件.

So read it as text, then call as.numeric(...) on the column. Or alter the file.

这篇关于R中的read.csv中指定colClasses的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆