gpt4 book ai didi

r - R中的日期时间转换错误-重复的第一时间点

转载 作者:行者123 更新时间:2023-12-03 08:01:31 25 4
gpt4 key购买 nike

R新手在这里。
我有一个包含时间戳的数据集。
例如:

d <- c("2017-09-17T10:01:23.0000000","2017-09-17T10:01:23.0100000","2017-09-17T10:01:23.0200000","2017-09-17T10:01:23.0300000","2017-09-17T10:01:23.0400000","2017-09-17T10:01:23.0500000","2017-09-17T10:01:23.0600000","2017-09-17T10:01:23.0700000","2017-09-17T10:01:23.0800000","2017-09-17T10:01:23.0900000")

我想将其读入R,所以我使用 as.POSIXct如下:
z <- as.POSIXct(d[1:10], format = "%Y-%m-%dT%H:%M:%OS")

结果如下:
"2017-09-17 10:01:23.00 CDT" "2017-09-17 10:01:23.00 CDT"** "2017-09-17 10:01:23.01 CDT" "2017-09-17 10:01:23.02 CDT" "2017-09-17 10:01:23.03 CDT" "2017-09-17 10:01:23.04 CDT" "2017-09-17 10:01:23.05 CDT" "2017-09-17 10:01:23.06 CDT" "2017-09-17 10:01:23.07 CDT" **"2017-09-17 10:01:23.08 CDT"

如您所见,转换后第一个时间点刚刚复制。

我究竟做错了什么?

谢谢你们。

更新:
我尝试了20个时间点,这确实发生了。
t <- c("2017-09-17T11:47:15.5000000","2017-09-17T11:47:15.5100000","2017-09-17T11:47:15.5200000","2017-09-17T11:47:15.5300000",
"2017-09-17T11:47:15.5400000","2017-09-17T11:47:15.5500000","2017-09-17T11:47:15.5600000","2017-09-17T11:47:15.5700000",
"2017-09-17T11:47:15.5800000","2017-09-17T11:47:15.5900000","2017-09-17T11:47:15.6000000","2017-09-17T11:47:15.6100000",
"2017-09-17T11:47:15.6200000","2017-09-17T11:47:15.6300000","2017-09-17T11:47:15.6400000","2017-09-17T11:47:15.6500000",
"2017-09-17T11:47:15.6600000","2017-09-17T11:47:15.6700000","2017-09-17T11:47:15.6800000","2017-09-17T11:47:15.6900000")

z <- as.POSIXct(t[1:20], format = "%Y-%m-%dT%H:%M:%OS")

结果
>"2017-09-17 11:47:15.50 CDT" "2017-09-17 11:47:15.50 CDT" "2017-09-17 11:47:15.51 CDT" "2017-09-17 11:47:15.52 CDT" "2017-09-17 11:47:15.53 CDT" "2017-09-17 11:47:15.54 CDT" "2017-09-17 11:47:15.55 CDT" "2017-09-17 11:47:15.56 CDT" "2017-09-17 11:47:15.57 CDT" "2017-09-17 11:47:15.58 CDT" "2017-09-17 11:47:15.59 CDT" "2017-09-17 11:47:15.60 CDT" **"2017-09-17 11:47:15.61 CDT" "2017-09-17 11:47:15.63 CDT"** "2017-09-17 11:47:15.64 CDT" "2017-09-17 11:47:15.65 CDT" "2017-09-17 11:47:15.66 CDT" "2017-09-17 11:47:15.67 CDT" "2017-09-17 11:47:15.68 CDT" "2017-09-17 11:47:15.69 CDT"

最佳答案

显然,打印表示形式无法处理z [1]的十进制表示形式不准确的事实:看一下两者的区别:

> z[1] - z[2]
Time difference of -0.00999999 secs

我认为print.POSIXct使用format.POSIXct,它确实使用了digits.secs选项,您可以尝试更改它。它并不能真正解决我机器上的问题,但就其效果而言,它是特定于操作系统的。
options(digits.secs = 6)

一个骇人听闻的事实表明它们并不相同
z+0.0001

[1] "2017-09-17 10:01:23.0000 PDT" "2017-09-17 10:01:23.0100 PDT" "2017-09-17 10:01:23.0200 PDT"
[4] "2017-09-17 10:01:23.0300 PDT" "2017-09-17 10:01:23.0400 PDT" "2017-09-17 10:01:23.0500 PDT"
[7] "2017-09-17 10:01:23.0600 PDT" "2017-09-17 10:01:23.0700 PDT" "2017-09-17 10:01:23.0800 PDT"
[10] "2017-09-17 10:01:23.0900 PDT"

关于r - R中的日期时间转换错误-重复的第一时间点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48308320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com