2016-02-21 18 views
1

當我使用下面的read.zoo時,它很好,直到我添加了最後一行: (我的源代碼是CSV,但是它的格式爲複製):read.zoo索引中的錯誤在數據行2有錯誤的輸入

library(zoo) 
Lines <- "fdatetime,Consumption 
    1,27/03/2015 01:00,0.04 
    2,27/03/2015 02:00,0.04" 


> z <- read.zoo(text = Lines, tz = "", format = "%d/%m/%Y %H:%M", sep = ",") 
Error in read.zoo(text = Lines, tz = "", format = "%d/%m/%Y %H:%M", sep = ",") : 
    index has bad entry at data row 51 

最後一行有什麼問題?如果你刪除最後一行,它將起作用!

> data.table::fread(file.choose(), verbose = TRUE) 
Input contains no \n. Taking this to be a filename to open 
File opened, filesize is 0.000001 GB. 
Memory mapping ... ok 
Detected eol as \r\n (CRLF) in that order, the Windows standard. 
Positioned on line 1 after skip or autostart 
This line is the autostart and not blank so searching up for the last non-blank ... line 1 
Detecting sep ... ',' 
Detected 3 columns. Longest stretch was from line 2 to line 30 
Starting data input on line 2 (either column names or first row of data). First 10 characters: 1,25/03/20 
Some fields on line 2 are not type character (or are empty). Treating as a data row and using default column names. 
Count of eol: 51 (including 0 at the end) 
Count of sep: 102 
nrow = MIN(nsep [102]/ncol [3] -1, neol [51] - nblank [0]) = 51 
Type codes ( first 5 rows): 143 
Type codes (+ middle 5 rows): 143 
Type codes (+ last 5 rows): 143 
Type codes: 143 (after applying colClasses and integer64) 
Type codes: 143 (after applying drop or select (if supplied) 
Allocating 3 column slots (3 - 0 dropped) 
Read 51 rows. Exactly what was estimated and allocated up front 
    0.000s ( 0%) Memory map (rerun may be quicker) 
    0.000s ( 0%) sep and header detection 
    0.000s ( 0%) Count rows (wc -l) 
    0.001s (100%) Column type detection (first, middle and last 5 rows) 
    0.000s ( 0%) Allocation of 51x3 result (xMB) in RAM 
    0.000s ( 0%) Reading data 
    0.000s ( 0%) Allocation for type bumps (if any), including gc time if triggered 
    0.000s ( 0%) Coercing data already read in type bumps (if any) 
    0.000s ( 0%) Changing na.strings to NA 
    0.001s  Total 
+0

你檢查,如果你的最後一行在一個\ r \ n(回車)結束?也許在文件末尾有一兩個空行。 – BerndGit

+0

以上對我而言(在Linux上)不會產生錯誤。這個問題很可能與@BerndGit指出的原始文件中的回車有關。 – nrussell

+0

可以看出沒有回車。我甚至在使用100甚至更多的線路時嘗試了它,問題出在這個特定的線路上。其他具有相同格式的文件效果很好。 – Avi

回答

2

感謝@Henrik, 它的解決方案是指定TZ,如下:

z<-read.zoo(ts1, tz = "UTC", format = "%d/%m/%Y %H:%M", sep = ",") 
+4

由於問題在於切換到夏令時,因此您可以交替使用chron代替POSIXct,因爲它沒有時區或夏令時:'library(chron); z < - read.zoo(text = Lines,FUN = as.chron,format =「%d /%m /%Y%H:%M」,sep =「,」) –