2012-05-28 29 views
3

我有這個時間序列數據:如何在xts中讀取時間序列?

 "timestamp"   "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom" 
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002 
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001 
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77 
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999 
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999 

我使用下面的代碼:

d <- read.table(Name[1], header=TRUE) #Name[1] is text file containing data 

d <- read.zoo(d, 
format="'%Y-%m-%d %H:%M:%S'", 
FUN=as.POSIXct ) 

它給我這個錯誤:

Error in read.zoo(d, format = "'%Y-%m-%d %H:%M:%S'", FUN = as.POSIXct) : 
index has 5 bad entries at data rows: 1 2 3 4 5 

我希望得到這個幫助問題。 謝謝您的考慮。

+0

在你的時代結束時,「-05」位是什麼? –

+0

@BenBolker準確地說。這就是導致這個問題的原因,因爲'as.POSIXct'不知道如何處理這個問題。 – Andrie

+0

@BenBolker它似乎是格式不正確的時間戳。看到我的答案。 – Andrie

回答

4

這適用於帖子中的數據,只要在每個日期/時間結束時忽略-05即可。 (從文件中使用類似註釋掉線讀取。)

Lines <- '"timestamp"   "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom" 
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002 
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001 
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77 
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999 
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999' 

library(zoo) 
# z <- read.zoo("myfile.txt", tz = "") 
z <- read.zoo(text = Lines, tz = "") 

從上面的代碼的輸出是:

> z 
        depth from_sensor_to_river_bottom Depth_from_river_surface_to_bottom 
2012-05-23 17:30:08 16.37      17.16        0.79 
2012-05-23 17:45:13 16.35      17.16        0.81 
2012-05-23 18:00:03 16.39      17.16        0.77 
2012-05-23 18:15:08 16.38      17.16        0.78 
2012-05-23 18:30:12 16.40      17.16        0.76 

欲瞭解更多信息嘗試?read.zoo?read.table,也vignette("zoo-read")。最後一個是關於給出read.zoo示例的整個文檔。

編輯:增加了評論的鏈接。

4

您的時間戳數據包含格式不正確的時區數據,即那些-05結尾到每個時間戳。

?strptime我知道您可以使用%z來格式化帶符號的時區偏移量,該偏移量應該是一個帶符號的四位數字,例如, -0500

%z 
Signed offset in hours and minutes from UTC, so -0800 is 8 hours behind UTC. 

所以,這裏是添加缺少的00到您的時間戳解決方法:

重建數據:

dat <- ' 
"timestamp" "depth" "from_sensor_to_river_bottom" "Depth_from_river_surface_to_bottom" 
"1" "2012-05-23 18:30:12-05" 16.4 17.16 0.760000000000002 
"2" "2012-05-23 18:15:08-05" 16.38 17.16 0.780000000000001 
"3" "2012-05-23 18:00:03-05" 16.39 17.16 0.77 
"4" "2012-05-23 17:45:13-05" 16.35 17.16 0.809999999999999 
"5" "2012-05-23 17:30:08-05" 16.37 17.16 0.789999999999999 
' 

添加缺少的零:

x <- read.table(text=dat, header=TRUE) 
x$timestamp <- paste(x$timestamp, "00", sep="") 
x$timestamp <- as.POSIXct(x$timestamp, format="%Y-%m-%d %H:%M:%S%z") 
x 

轉換到動物園

library(zoo) 
as.zoo(x) 
    timestamp   depth from_sensor_to_river_bottom Depth_from_river_surface_to_bottom 
1 2012-05-24 00:30:12 16.40 17.16      0.76        
2 2012-05-24 00:15:08 16.38 17.16      0.78        
3 2012-05-24 00:00:03 16.39 17.16      0.77        
4 2012-05-23 23:45:13 16.35 17.16      0.81        
5 2012-05-23 23:30:08 16.37 17.16      0.79 
+0

但這已改變了時間系列值: 查看我的數據中的時間戳值和您獲得的值 – rockswap

+0

我不認爲它有。數據顯示5小時偏移與UTC。動物園對象打印UTC時間,這是正確的。如果你想要不同的行爲,你可以很容易地使用類似的邏輯從本地時間的數據顯示中刪除'-05'字符串。 – Andrie

+0

好的..謝謝你。 我想減去兩個時間值。我試着用這個代碼來做,並失敗。如何才能成爲可能? – rockswap

相關問題