【问题标题】:How to find different elements of two time vectors?如何找到两个时间向量的不同元素?
【发布时间】:2016-12-24 08:34:32
【问题描述】:

考虑这两个时间向量:

a<-seq(as.POSIXct("2010-01-01 05:00:00"), as.POSIXct("2010-01-02 23:55:00"), by = '5 min')
b<-seq(as.POSIXct("2010-01-01 00:00:00"), as.POSIXct("2010-01-03 23:55:00"), by = '10 min')

如何获取这两个向量之间的不同元素?我试过了:

union(setdiff(a, b), setdiff(b, a))

但是返回的值不是时间格式的。

【问题讨论】:

  • as.POSIXct(union(setdiff(a, b), setdiff(b, a)),origin="1970-01-01") 将返回时间格式..
  • 它有效,但origin="1970-01-01" 是什么意思?
  • 这是时间格式的默认来源。

标签: r vector time difference


【解决方案1】:

这仅使用保留"POSIXct" 类的操作:

c(a[!a %in% b], b[!b %in% a])

【讨论】:

    【解决方案2】:

    这也可以(使用默认原点):

    as.POSIXct(union(setdiff(a, b), setdiff(b, a)), origin = '1970-01-01')
    
    #[1] "2010-01-01 05:05:00 IST" "2010-01-01 05:15:00 IST" "2010-01-01 05:25:00 IST" "2010-01-01 05:35:00 IST" "2010-01-01 05:45:00 IST"
    #[6] "2010-01-01 05:55:00 IST" "2010-01-01 06:05:00 IST" "2010-01-01 06:15:00 IST" "2010-01-01 06:25:00 IST" "2010-01-01 06:35:00 IST"
    
    # this checks a U b = (a - b) U (b - a) U (a /\ b) for PoSIxct objects, should evaluate to true
    all(sort(as.POSIXct(union(union(setdiff(a, b), setdiff(b, a)), intersect(a, b)), origin = '1970-01-01')) == sort(as.POSIXct(union(a, b), origin = '1970-01-01')))
    # TRUE
    

    【讨论】:

    • 我喜欢你的回答,因为你很棒!!!我正在向你学习,你摇滚!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-14
    • 2012-03-07
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多