如何使用 POSIXct 变量合并

How to merge using POSIXct variable

我正在尝试通过 POSIXct 列合并两个数据框。我正在使用 tidyverse 和 lubridate。 inner_join() returns 空数据帧,而 full_join() returns 重复较短的框架。这是一个错误还是有更合适或更喜欢的方法来加入 POSIX 个对象?

> library(tidyverse)
> library(lubridate)

> d1 <- tibble(date = as.POSIXct(seq(Sys.time() - days(1), Sys.time(), by = "min"), tz = "America/New_York"))
> head(d1)
# A tibble: 6 x 1
  date               
  <dttm>             
1 2018-03-09 14:01:34
2 2018-03-09 14:02:34
3 2018-03-09 14:03:34
4 2018-03-09 14:04:34
5 2018-03-09 14:05:34
6 2018-03-09 14:06:34

> d2 <- tibble(date = as.POSIXct(seq(Sys.time() - days(2), Sys.time(), by = "min"), tz = "America/New_York"))
> head(d2)
# A tibble: 6 x 1
  date               
  <dttm>             
1 2018-03-08 14:01:55
2 2018-03-08 14:02:55
3 2018-03-08 14:03:55
4 2018-03-08 14:04:55
5 2018-03-08 14:05:55
6 2018-03-08 14:06:55

> data <- inner_join(d1, d2, by = "date")
> head(data)
# A tibble: 0 x 1
# ... with 1 variable: date <dttm>

> data <- inner_join(d1, d2, by = "date")
> head(data)
# A tibble: 0 x 1
# ... with 1 variable: date <dttm>

> data <- merge(d1, d2, by = "date")
> dim(data)
[1] 0 1

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

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

other attached packages:
 [1] lubridate_1.7.3 forcats_0.3.0   stringr_1.3.0   dplyr_0.7.4     purrr_0.2.4     readr_1.1.1     tidyr_0.8.0    
 [8] tibble_1.4.2    ggplot2_2.2.1   tidyverse_1.2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.15     cellranger_1.1.0 pillar_1.2.1     compiler_3.4.2   plyr_1.8.4       bindr_0.1       
 [7] tools_3.4.2      jsonlite_1.5     nlme_3.1-131.1   gtable_0.2.0     lattice_0.20-35  pkgconfig_2.0.1 
[13] rlang_0.2.0      psych_1.7.8      cli_1.0.0        rstudioapi_0.7   yaml_2.1.18      parallel_3.4.2  
[19] haven_1.1.1      bindrcpp_0.2     xml2_1.2.0       httr_1.3.1       hms_0.4.1        grid_3.4.2      
[25] glue_1.2.0       R6_2.2.2         readxl_1.0.0     foreign_0.8-69   modelr_0.1.1     reshape2_1.4.3  
[31] magrittr_1.5     scales_0.5.0     rvest_0.3.2      assertthat_0.2.0 mnormt_1.5-5     colorspace_1.3-2
[37] stringi_1.1.6    lazyeval_0.2.1   munsell_0.4.3    broom_0.4.3      crayon_1.3.4

我们中的许多人为此使用了 zoo 然后 xts,并且在此处和其他地方的网站上有 许多 的答案.

下面是一些展示该方法的代码:

library(xts)

t1 <- as.POSIXct(seq(Sys.time() - 24*60*60, Sys.time(), by = "min"),
                 tz = "America/New_York")
d1 <- xts(rep(1,length(t1)), order.by=t1)

t2 <- as.POSIXct(seq(Sys.time() - 2*24*60*60, Sys.time(), by = "min"),
                 tz = "America/New_York")
d2 <- xts(rep(2,length(t2)), order.by=t2)

x <- merge(d1,d2)
summary(x)

结果是

R> summary(x)
     Index                              d1             d2      
 Min.   :2018-03-08 13:56:30.68   Min.   :1      Min.   :2     
 1st Qu.:2018-03-09 07:56:45.68   1st Qu.:1      1st Qu.:2     
 Median :2018-03-09 19:56:13.93   Median :1      Median :2     
 Mean   :2018-03-09 17:56:22.83   Mean   :1      Mean   :2     
 3rd Qu.:2018-03-10 04:56:22.30   3rd Qu.:1      3rd Qu.:2     
 Max.   :2018-03-10 13:56:30.68   Max.   :1      Max.   :2     
                                  NA's   :2881   NA's   :1441  
R> 

注意两列中的 NA。 POSIXct 是自纪元以来的 小数 秒,因此除非截断,否则无法轻松合并。即便如此,FAQ 7.31 也可能会抬起头来。

也有 data.table 解决方案,但您需要小心小数秒。 ITime 可以提供帮助。