2011-05-23 20 views
1

我對Hadley的「rbind.fill」函數的行爲感到困惑。我有一個數據框的列表,我想對其執行一個簡單的rbind操作,但是rbind.fill函數給了我無法解釋的結果。請注意,「rbind」函數確實給了我期望的輸出。下面是小例子:意外的「rbind.fill」行爲

library(reshape)  
data1 <- structure(list(DATE = structure(c(1277859600, 1277856000), class = c("POSIXct", 
        "POSIXt"), tzone = "GMT"), BACK = c(0, -1)), .Names = c("DATE", 
        "BACK"), row.names = 1:2, class = "data.frame") 
data2 <- structure(list(DATE = structure(c(1277856000, 1277852400), class = c("POSIXct", 
        "POSIXt"), tzone = "GMT"), BACK = c(0, -1)), .Names = c("DATE", 
        "BACK"), row.names = 1:2, class = "data.frame") 
bind1 <- rbind.fill(list(data1, data2)) 
bind2 <- rbind(data1, data2) 
data1 
data2 
bind1 
bind2 
       DATE BACK 
1 2010-06-30 01:00:00 0 
2 2010-06-30 00:00:00 -1 
       DATE BACK 
1 2010-06-30 00:00:00 0 
2 2010-06-29 23:00:00 -1 
       DATE BACK 
1 2010-06-29 18:00:00 0 
2 2010-06-29 17:00:00 -1 
3 2010-06-29 17:00:00 0 
4 2010-06-29 16:00:00 -1 
       DATE BACK 
1 2010-06-30 01:00:00 0 
2 2010-06-30 00:00:00 -1 
3 2010-06-30 00:00:00 0 
4 2010-06-29 23:00:00 -1 

因此,大家可以看到,bind1其中包含rbind.fill輸出會在DATE列甚至沒有在原數據集中的新時代。這是預期的行爲?我知道我可以簡單地使用
bind <- do.call(rbind, list(data1, data2))
來綁定我擁有的5000+數據框,但任何人都可以對上述行爲說話嗎?
謝謝。正如@DWin指出的,這不是rbind.fill函數本身的問題,但是在輸出中,時間是在太平洋時間打印的,但是格式是GMT格式。

SessionInfo() 
R version 2.12.1 (2010-12-16) 
Platform: x86_64-pc-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C       
[5] LC_TIME=English_United States.1252  

attached base packages: 
[1] tcltk  grid  stats  graphics grDevices utils  datasets methods 
[9] base  

other attached packages: 
[1] tcltk2_1.1-5 reshape_0.8.4 plyr_1.4  proto_0.3-9.1 

loaded via a namespace (and not attached): 
[1] ggplot2_0.8.9 tools_2.12.1 
+1

我無法複製此行爲。你的'sessionInfo()'是什麼? – 2011-05-23 17:39:46

+0

看起來,正如DWin指出的那樣(以及您似乎期待的),這是一個時區問題,並且僅與rbind.fill函數本身(我假設它在某處內使用「print.POSIXct」)間接相關 – Rguy 2011-05-23 18:22:19

回答

2

最有可能的是您看到的是print.POSIXct與您的機器上的時區設置進行交互的行爲。我爲兩個函數調用獲得完全相同的輸出。

> rbind.fill(list(data1,data2)) == rbind(data1,data2) 
    DATE BACK 
1 TRUE TRUE 
2 TRUE TRUE 
3 TRUE TRUE 
4 TRUE TRUE 
> identical(rbind.fill(list(data1,data2)) , rbind(data1,data2)) 
[1] TRUE 

我相當確定POSIXct時間默認爲GMT。需要注意的是as.POSIXt有TZ說法:

tz A timezone specification to be used for the conversion, if one is required. 
    System-specific (see time zones), but "" is the current timezone, and "GMT" is 
    UTC (Universal Time, Coordinated). 

如果鍵入?locales,你會看到的功能來獲取和設置區域設置雖然這些從操作系統OS不同,所以我在Mac上的經驗可能不符合你的不同的操作系統。我嘗試使用Date類而不是POSIX類,但這只是因爲我沒有特別需要添加時間級別的細節。您可能需要檢查chronlubridate包中的其他功能。

+0

是,這似乎是問題所在。感謝您的答覆。數據格式爲GMT格式,轉換爲PST(我當前的時區)作品會在輸出中生成這些時間。我想我需要更多的意識到這些事情,而不是隻相信魔術盒。有關如何避免此類問題的任何建議? – Rguy 2011-05-23 18:20:13