在R中使用'na.strings'和'fClass'参数读取数据时错误的列模式

时间:2014-09-08 13:01:00

标签: r data.table fread

Windows 8.1,R版本3.1.1(2014-07-10),System x86_64,mingw32

我有一个包含大量观察结果的文件(here)。以下是文件中的一些字符串

Date;Time;Global_active_power;Global_reactive_power;Voltage;Global_intensity;Sub_metering_1;Sub_metering_2;Sub_metering_3
16/12/2006;17:24:00;4.216;0.418;234.840;18.400;0.000;1.000;17.000
16/12/2006;17:25:00;5.360;0.436;233.630;23.000;0.000;1.000;16.000
28/4/2007;00:20:00;0.492;0.208;236.240;2.200;0.000;0.000;0.000
28/4/2007;00:21:00;?;?;?;?;?;?;
21/12/2006;11:25:00;0.246;0.000;241.740;1.000;0.000;0.000;0.000
21/12/2006;11:26:00;0.246;0.000;241.830;1.000;0.000;0.000;0.000

NA值由"?"表示。我试图用

读取文件
epcData <- fread(dataFile,
                 sep = ";",
                 header = TRUE,
                 na.strings = "?",
                 colClasses = c("character", "character", rep("numeric", 7)),
                 stringsAsFactors = FALSE)

我收到了警告:

Bumped column 3 to type character on data row 10, field contains '?'. Coercing previously read values in this column from integer or numeric back to character which may not be lossless; e.g., if '00' and '000' occurred before they will now be just '0', and there may be inconsistencies with treatment of ',,' and ',NA,' too (if they occurred in this column before the bump). If this matters please rerun and set 'colClasses' to 'character' for this column. Please note that column type detection uses the first 5 rows, the middle 5 rows and the last 5 rows, so hopefully this message should be very rare. If reporting to datatable-help, please rerun and include the output from verbose=TRUE.

第10行是

   28/4/2007;00:21:00;?;?;?;?;?;?;
  

epcData [10]

打印

         Date     Time Global_active_power Global_reactive_power Voltage
1: 28/4/2076 00:21:00                  NA                    NA      NA
   Global_intensity Sub_metering_1 Sub_metering_2 Sub_metering_3
1:               NA             NA             NA             NA

但是所有列的模式都是&#34;字符&#34; ,即使是第3:9列(但是colClasses = c(&#34;字符&#) 34;,&#34;字符&#34;,rep(&#34;数字&#34;,7)))。

出了什么问题?

1 个答案:

答案 0 :(得分:2)

截止到今天,data.table程序包的版本为1.12.2。这不再是问题,并且以上csv数据的导入都可以正常工作,并且所有问号都由NA s

代替