意外的“rbind.fill”行为

时间:2011-05-23 17:33:41

标签: datetime r reshape rbind

我对Hadley的“rbind.fill”功能的行为感到困惑。我有一个数据框列表,我想做一个简单的rbind操作,但rbind.fill函数给我的结果,我无法解释。请注意,“rbind”函数确实给出了我期望的输出。这是最小的例子:

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 个答案:

答案 0 :(得分: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

我有理由相信,GMT默认情况下POSIXct时间是默认的。请注意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,您将看到获取和设置区域设置的功能,尽管这些功能因操作系统而异,因此我在Mac上的体验可能与您在其他操作系统上的体验不相符。我尝试使用Date类而不是POSIX类,但这只是因为我没有特别需要增加的时间级别细节。您可能希望检查的chronlubridate包中还有其他功能。

相关问题