在dplyr连接中,润滑间隔会丢失

时间:2017-02-10 17:32:33

标签: r dplyr lubridate

Lubridate间隔在dplyr join()中丢失,但在基础merge()中丢失:

library(dplyr)
library(lubridate)

(foo <- 
    tibble(
      start = seq(as.Date("2016-01-01"), by = 1, length.out = 2),
      end = seq(as.Date("2016-01-02"), by = 2, length.out = 2)
    ) %>% 
    transmute(itv = interval(start, end), dummy = 1))
# # A tibble: 2 × 2
#                              itv dummy
#                   <S4: Interval> <dbl>
# 1 2016-01-01 UTC--2016-01-02 UTC     1
# 2 2016-01-02 UTC--2016-01-04 UTC     1

NA来自哪里?

foo %>% 
  inner_join(., ., by = "dummy")
# A tibble: 4 × 3
#                            itv.x dummy                          itv.y
#                   <S4: Interval> <dbl>                 <S4: Interval>
# 1 2016-01-01 UTC--2016-01-02 UTC     1 2016-01-01 UTC--2016-01-02 UTC
# 2 2016-01-02 UTC--2016-01-03 UTC     1 2016-01-02 UTC--2016-01-04 UTC
# 3                         NA--NA     1                         NA--NA
# 4                         NA--NA     1                         NA--NA

基础merge()看起来很好:

foo %>% 
  merge(., ., by = "dummy")
#   dummy                          itv.x                          itv.y
# 1     1 2016-01-01 UTC--2016-01-02 UTC 2016-01-01 UTC--2016-01-02 UTC
# 2     1 2016-01-01 UTC--2016-01-02 UTC 2016-01-02 UTC--2016-01-04 UTC
# 3     1 2016-01-02 UTC--2016-01-04 UTC 2016-01-01 UTC--2016-01-02 UTC
# 4     1 2016-01-02 UTC--2016-01-04 UTC 2016-01-02 UTC--2016-01-04 UTC

这可能是个错误吗?或者只是我对Interval S4课程做错了什么?

sessionInfo()
# R version 3.3.2 (2016-10-31)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Linux Mint 18
# 
# locale:
#  [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C               LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8     LC_MONETARY=fr_FR.UTF-8   
#  [6] LC_MESSAGES=fr_FR.UTF-8    LC_PAPER=fr_FR.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
# [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] lubridate_1.6.0 dplyr_0.5.0    
# 
# loaded via a namespace (and not attached):
# [1] magrittr_1.5   R6_2.2.0       assertthat_0.1 DBI_0.5-1      tools_3.3.2    tibble_1.2     Rcpp_0.12.9    stringi_1.1.2  stringr_1.1.0

0 个答案:

没有答案