我有一个数字向量,代表自1970年1月1日以来的毫秒数。我想使用lubridate
将其转换为日期时间对象。数据示例如下:
raw_times <- c(1139689917479, 1139667123031, 1140364113915, 1140364951003,
1139643685434, 1139677091970, 1139691963511, 1140339448413, 1140368308429,
1139686613641, 1139666081813, 1140351488730, 1140346617958, 1141933663183,
1141933207579, 1140360125149, 1140351845108, 1140365079103, 1141933549825,
1140365601476)
知道
as_date
和as_datetime
的文档指示它们采用了代表从1970年1月1日以来的天数的数字矢量,我尝试了以下操作:library(lubridate)
as_date(raw_times / (1000 * 60 * 60 * 24))
"2006-02-11" "2006-02-11" "2006-02-19" "2006-02-19" "2006-02-11"
"2006-02-11" "2006-02-11" "2006-02-19" "2006-02-19" "2006-02-11"
"2006-02-11" "2006-02-19" "2006-02-19" "2006-03-09" "2006-03-09"
"2006-02-19" "2006-02-19" "2006-02-19" "2006-03-09" "2006-02-19"
(显然是利用一天每秒1000毫秒,一分钟60秒,一小时60分钟,一天24小时的事实。)
当我用
as_datetime
运行相同的代码时,得到以下信息:as_datetime(raw_times / (1000 * 60 * 60 * 24))
"1970-01-01 03:39:50 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:50 UTC"
"1970-01-01 03:39:50 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:50 UTC" "1970-01-01 03:39:58 UTC"
"1970-01-01 03:39:58 UTC" "1970-01-01 03:40:16 UTC" "1970-01-01 03:40:16 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC" "1970-01-01 03:39:58 UTC"
"1970-01-01 03:40:16 UTC" "1970-01-01 03:39:58 UTC"
结果是不同的。我以为我还缺少其他一些论点,但是我在文档中找不到任何可以告诉我那是什么的内容。
会话信息如下:
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.6.0
loaded via a namespace (and not attached):
[1] magrittr_1.5 tools_3.3.2 stringi_1.1.2 stringr_1.1.0
最佳答案
不是(已删除程序包名称的)解决方案,但是您可以使用base::.POSIXct
来做到这一点:
R> options(digits.secs=3)
R> .POSIXct(raw_times/1000)
[1] "2006-02-11 14:31:57.479 CST" "2006-02-11 08:12:03.030 CST"
[3] "2006-02-19 09:48:33.914 CST" "2006-02-19 10:02:31.003 CST"
[5] "2006-02-11 01:41:25.434 CST" "2006-02-11 10:58:11.970 CST"
[7] "2006-02-11 15:06:03.510 CST" "2006-02-19 02:57:28.413 CST"
[9] "2006-02-19 10:58:28.428 CST" "2006-02-11 13:36:53.641 CST"
[11] "2006-02-11 07:54:41.812 CST" "2006-02-19 06:18:08.730 CST"
[13] "2006-02-19 04:56:57.957 CST" "2006-03-09 13:47:43.183 CST"
[15] "2006-03-09 13:40:07.578 CST" "2006-02-19 08:42:05.148 CST"
[17] "2006-02-19 06:24:05.108 CST" "2006-02-19 10:04:39.102 CST"
[19] "2006-03-09 13:45:49.825 CST" "2006-02-19 10:13:21.476 CST"
关于r - 润滑as_date和。 as_datetime行为差异,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40959726/