在 read.csv 中指定 colClasses [英] Specifying colClasses in the read.csv

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

问题描述

我试图在 R 的 read.csv 函数中指定 colClasses 选项.在我的数据中,第一列时间"基本上是一个字符向量,而其余的列是数字.

I am trying to specify the colClasses options in the read.csv function in R. In my data, the first column "time" is basically a character vector while the rest of the columns are numeric.

data <- read.csv("test.csv", comment.char="" , 
                 colClasses=c(time="character", "numeric"), 
                 strip.white=FALSE)

在上面的命令中,我希望 R 在时间"列中读取为字符",其余为数字.尽管在命令完成后data"变量确实有正确的结果,但 R 返回了以下警告.我想知道如何解决这些警告?

In the above command, I would want R to read in the "time" column as "character" and the rest as numeric. Although, the "data" variable did have the correct result after the command completed, R returned the following warnings. I am wondering how I could fix these warnings?

Warning messages:
 1: In read.table(file = file, header = header, sep = sep, quote = quote,  :
    not all columns named in 'colClasses' exist
 2: In tmp[i[i > 0L]] <- colClasses :
    number of items to replace is not a multiple of replacement length

德里克

推荐答案

colClasses 向量的长度必须等于导入的列数.假设您的数据集列的 rest 是 5:

The colClasses vector must have length equal to the number of imported columns. Supposing the rest of your dataset columns are 5:

colClasses=c("character",rep("numeric",5))

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

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