readr :: read_csv(" file.csv",col_types = rep(" c",times = 18))给出错误:与STRSXP不兼容

时间:2016-01-05 15:39:44

标签: r csv readr

我试图读取一个相当简单的csv文件,但是当我尝试指定列类型时,readr会抛出错误。这是我的数据的一小部分:

text <- "Item,Date,Time,SeizureTime,ET,OriginatingNumber,TerminatingNumber,IMEI,IMSI,CT,Feature,DIALED,FORWARDED,TRANSLATED,ORIG_ORIG,MAKE,MODEL,TargetNumber
3,10/31/2012,7:53:00,0:15,1:43,(123)555-1216,(123)555-5662,,,MO,[],,11235552511,,,,,(123)555-5662
4,10/31/2011,9:04:00,0:25,0:00,(123)555-0214,(123)555-5662,,,MO,[],,11235552511,,,,,(123)555-5662
9,10/31/2014,9:08:00,0:11,2:13,(123)555-8555,(132)555-5662,,,MO,[],,11235552511,,,,,(123)555-5662
12,10/31/2011,9:27:00,0:07,0:10,(123)555-0214,(123)555-5662,,,MO,[],,11235552511,,,,,(123)555-5662
13,10/31/2015,9:35:00,0:27,0:00,(123)555-0214,(123)555-5662,,,MO,[],,11235552511,,,,,(123)555-5662
16,10/31/2011,10:09:00,0:10,14:13,(123)555-1216,(123)555-5662,,,MO,[],,11235552511,,,,,(123)555-5662"

dat <- read.table(text = text, sep = ",", header = TRUE)

尝试使用read_csv("file.csv", col_types = rep("c", times = 18))读取实际的CSV时,我收到标题中提到的错误。我已经看到了关于这个错误的几个SO问题,它们似乎都与后台发生的C ++函数有关,但我不知道如何解决它。如果我剥离col_types参数,则错误消失,但它没有正确解析数据。

1 个答案:

答案 0 :(得分:5)

当您指定使用rep("c", times=18)的列时,会创建一个包含18个元素的向量。在?read_csv的帮助中,col_types参数采用单个字符串快捷键字符串,如"ccccdc"。所以我们将c粘贴在一起形成一个字符串:

read_csv(text, col_types=paste(rep("c", 18), collapse=""))
Source: local data frame [6 x 18]

   Item       Date     Time SeizureTime    ET
  (chr)      (chr)    (chr)       (chr) (chr)
1     3 10/31/2012  7:53:00        0:15  1:43
2     4 10/31/2011  9:04:00        0:25  0:00
3     9 10/31/2014  9:08:00        0:11  2:13
4    12 10/31/2011  9:27:00        0:07  0:10
5    13 10/31/2015  9:35:00        0:27  0:00
6    16 10/31/2011 10:09:00        0:10 14:13