如何让 POSIXct 与 == 一起工作
How to get POSIXct to work with ==
我注意到 ==
似乎不适用于 POSIXct:
a <- structure(c(1466985600, 1464652800, 1464652800, 1464652800, 1472428800,
1466985600, 1468195200, 1472428800, 1472428800, 1474848000, 1464652800,
1468195200, 1476057600, 1464652800, 1472428800, 1472428800, 1460332800,
1460332800, 1460332800, 1460332800), class = c("POSIXct", "POSIXt"
), tzone = "UTC")
a
[1] "2016-06-27 UTC" "2016-05-31 UTC" "2016-05-31 UTC" "2016-05-31 UTC" "2016-08-29 UTC" "2016-06-27 UTC" "2016-07-11 UTC"
"2016-08-29 UTC"
[9] "2016-08-29 UTC" "2016-09-26 UTC" "2016-05-31 UTC" "2016-07-11 UTC" "2016-10-10 UTC" "2016-05-31 UTC" "2016-08-29 UTC"
"2016-08-29 UTC"
[17] "2016-04-11 UTC" "2016-04-11 UTC" "2016-04-11 UTC" "2016-04-11 UTC"
table(a=="2016-06-27")
FALSE
20
table(a=="2016-06-27 UTC")
FALSE
20
table(a==as.POSIXct("2016-06-27 UTC"))
FALSE
20
table(a==as.POSIXct("2016-06-27"))
FALSE
20
a[a=="2016-06-27"]
character(0)
我怎样才能让它工作?
这可行,但我真的希望这不是我唯一的选择:
a[a==1466985600]
[1] "2016-06-27 UTC" "2016-06-27 UTC"
时区需要分开,除非在format
参数中指定。您的代码在字符串中嵌入时区,但默认格式为 "%Y-%m-%d"
和 "%Y/%m/%d"
,因此时区被截断并默认为当前语言环境。我们可以用 attr()
找到 a
的时区,因为它是 a
.
的属性
table(a == as.POSIXct("2016-06-27", tz = attr(a, "tzone")))
# FALSE TRUE
# 18 2
我注意到 ==
似乎不适用于 POSIXct:
a <- structure(c(1466985600, 1464652800, 1464652800, 1464652800, 1472428800,
1466985600, 1468195200, 1472428800, 1472428800, 1474848000, 1464652800,
1468195200, 1476057600, 1464652800, 1472428800, 1472428800, 1460332800,
1460332800, 1460332800, 1460332800), class = c("POSIXct", "POSIXt"
), tzone = "UTC")
a
[1] "2016-06-27 UTC" "2016-05-31 UTC" "2016-05-31 UTC" "2016-05-31 UTC" "2016-08-29 UTC" "2016-06-27 UTC" "2016-07-11 UTC" "2016-08-29 UTC" [9] "2016-08-29 UTC" "2016-09-26 UTC" "2016-05-31 UTC" "2016-07-11 UTC" "2016-10-10 UTC" "2016-05-31 UTC" "2016-08-29 UTC" "2016-08-29 UTC" [17] "2016-04-11 UTC" "2016-04-11 UTC" "2016-04-11 UTC" "2016-04-11 UTC"
table(a=="2016-06-27")
FALSE 20
table(a=="2016-06-27 UTC")
FALSE 20
table(a==as.POSIXct("2016-06-27 UTC"))
FALSE 20
table(a==as.POSIXct("2016-06-27"))
FALSE 20
a[a=="2016-06-27"]
character(0)
我怎样才能让它工作?
这可行,但我真的希望这不是我唯一的选择:
a[a==1466985600]
[1] "2016-06-27 UTC" "2016-06-27 UTC"
时区需要分开,除非在format
参数中指定。您的代码在字符串中嵌入时区,但默认格式为 "%Y-%m-%d"
和 "%Y/%m/%d"
,因此时区被截断并默认为当前语言环境。我们可以用 attr()
找到 a
的时区,因为它是 a
.
table(a == as.POSIXct("2016-06-27", tz = attr(a, "tzone")))
# FALSE TRUE
# 18 2