R 将unixtime转换为保留毫秒的datetime
我正在使用一些时间戳:R 将unixtime转换为保留毫秒的datetime,r,R,我正在使用一些时间戳: pageviews$timestamp %>% head() [1] "1605859226452" "1605859226461" "1605859248803" "1605859261112" "1605859283839" "1605859471370" x timestamp 1 1605859226452 2 160585
pageviews$timestamp %>% head()
[1] "1605859226452" "1605859226461" "1605859248803" "1605859261112" "1605859283839" "1605859471370"
x
timestamp
1 1605859226452
2 1605859226461
3 1605859248803
4 1605859261112
5 1605859283839
6 1605859471370
我需要将这些转换为datetimes,并尽可能地保留到最细粒度的级别,我认为是毫秒级
先前尝试使用as.POSIXct:
strtime_to_dt <- function(x) {
as.POSIXct(as.numeric(x) / 1000, origin = "1960-01-01", formats = "%Y-%m-%d %H:%M:%OS")
}
x <- pageviews$timestamp %>% head() # same data as above
> strtime_to_dt(x)
[1] "2010-11-20 08:00:26 UTC" "2010-11-20 08:00:26 UTC" "2010-11-20 08:00:48 UTC" "2010-11-20 08:01:01 UTC" "2010-11-20 08:01:23 UTC"
[6] "2010-11-20 08:04:31 UTC"
更新功能:
strtime_to_dt <- function(x) {format(strtime_to_dt(x), "%F %H:%M:%OS3")}
strtime_to_dt(x)
Error: C stack usage 7969908 is too close to the limit
我们能做到
format(strtime_to_dt(as.character(x$timestamp)), "%F %H:%M:%OS3")
#[1] "2010-11-20 02:00:26.451" "2010-11-20 02:00:26.460" "2010-11-20 02:00:48.802" "2010-11-20 02:01:01.111" "2010-11-20 02:01:23.838"
#[6] "2010-11-20 02:04:31.369"
数据
x您可以指定OS3
Hi@akrun,OK。。。哪里怎样?!已尝试将%Y-%m-%d%H:%m:%OS“
更改为%Y-%m-%d%H:%OS3”
和%Y-%m-%d%H:%m:OS”
这两种方法都没有做过。如果您需要将输出显示为字符,则可以使用strftime
或format
并显示毫秒。代码是格式(strime\u to\u dt(str1),%F%H:%M:%OS3”)
HI,尝试:x%head>x[1]“1605859226452”“1605859226461”“1605859248803”“1605859261112”“1605859283839”“1605859471370”>格式(strtime_to_dt(as.character(x)),%F%H:%M:%OS3”)错误:C堆栈使用量7972244太接近极限
@DougFir没关系,它对我来说工作正常:(我也尝试过使用这个结构(列表),但也出现了同样的错误。我猜这是我的集合所独有的。)up@DougFir你能尝试一个新的R会话吗?我正在MacYes上使用R4.0.3。我现在将尝试一个新会话。我也发布了sessionInfo()
format(strtime_to_dt(as.character(x$timestamp)), "%F %H:%M:%OS3")
#[1] "2010-11-20 02:00:26.451" "2010-11-20 02:00:26.460" "2010-11-20 02:00:48.802" "2010-11-20 02:01:01.111" "2010-11-20 02:01:23.838"
#[6] "2010-11-20 02:04:31.369"
x <- structure(list(timestamp = c(1605859226452, 1605859226461, 1605859248803,
1605859261112, 1605859283839, 1605859471370)), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))