【问题标题】:Unexpected "rbind.fill" behavior意外的“rbind.fill”行为
【发布时间】:2011-05-23 17:33:40
【问题描述】:

我对 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

如您所见,包含rbind.fill 输出的bind1 在DATE 列中创建了甚至在原始数据集中都不存在的新时间。这是预期的行为吗?我知道我可以简单地使用
bind &lt;- 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 

【问题讨论】:

  • 我无法复制这种行为。你的sessionInfo() 是什么?
  • 看来,正如 DWin 指出的那样(正如您所期望的那样)这是一个时区问题,并且仅与 rbind.fill 函数本身间接相关(我假设它使用“print.POSIXct “在某处?)

标签: datetime r reshape rbind


【解决方案1】:

您看到的很可能是 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 ,您将看到获取和设置区域设置的功能,尽管这些功能因操作系统而异,因此我在 Mac 上的体验可能与您在不同操作系统上的体验不同。我尝试使用 Date 类而不是 POSIX 类,但这只是因为我没有特别需要添加的时间级别细节。 chron 和 lubridate 包中还有其他功能,您可能想检查一下。

【讨论】:

  • 是的,这似乎是问题所在。谢谢您的答复。数据采用 GMT 格式,转换为 PST(我当前的时区)可以在输出中产生这些时间。我想我需要更加意识到这些事情,而不是仅仅相信魔法盒。关于如何避免此类问题的任何建议?
猜你喜欢
  • 1970-01-01
  • 2020-10-04
  • 2016-07-16
  • 2016-05-10
  • 2020-07-23
  • 2021-08-23
  • 2021-11-16
  • 2017-10-20
  • 2016-06-29
相关资源
最近更新 更多