I'm trying to read in a fairly simple csv file, but readr
is throwing an error when I try to specify the column types. Here's a small snippet of my data:
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)
When trying to read in the actual CSV using read_csv("file.csv", col_types = rep("c", times = 18))
I get the error mentioned in the title. I've seen a couple SO questions regarding this error and they all seem to be related to C++ functions taking place in the background, but I don't know how to remedy it. If I strip off the col_types
argument, the error goes away, but it doesn't parse the data correctly.
When you specified the columns you used rep("c", times=18)
, but that creates a vector with 18 elements. From the help for ?read_csv
, the col_types
argument takes a single string of column shortcuts like "ccccdc"
. So we paste the c's together to form one string:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With