我正在尝试将一些文本数据转换为时间。我知道将2019-09-24 00:00:03
转换为时间格式的方法,但是我想将频率较高的数据转换为时间。
一些观察结果如下:
[1] "2019-09-24D00:00:03.384309000" "2019-09-24D00:00:03.404018000"
[3] "2019-09-24D00:00:03.408065000" "2019-09-24D00:00:03.552361000"
[5] "2019-09-24D00:00:03.572690000" "2019-09-24D00:00:03.572690000"
[7] "2019-09-24D00:00:03.572690000" "2019-09-24D00:00:03.572690000"
我找到了一些可以转换此数据的Python代码:
data[‘timestamp’] = data.timestamp.map(lambda t: datetime.strptime(t[:-3], “%Y-%m-%dD%H:%M:%S.%f”))
数据:
data <- c("2019-09-24D00:00:03.384309000", "2019-09-24D00:00:03.404018000",
"2019-09-24D00:00:03.408065000", "2019-09-24D00:00:03.552361000",
"2019-09-24D00:00:03.572690000", "2019-09-24D00:00:03.572690000",
"2019-09-24D00:00:03.572690000", "2019-09-24D00:00:03.572690000",
"2019-09-24D00:00:03.572690000", "2019-09-24D00:00:03.572690000",
"2019-09-24D00:00:03.572690000", "2019-09-24D00:00:03.572690000",
"2019-09-24D00:00:03.572690000", "2019-09-24D00:00:03.572690000",
"2019-09-24D00:00:03.614967000", "2019-09-24D00:00:03.656928000",
"2019-09-24D00:00:03.711825000", "2019-09-24D00:00:03.711825000",
"2019-09-24D00:00:03.711825000", "2019-09-24D00:00:03.711825000"
)
答案 0 :(得分:1)
我们可以使用lubridate
来正确解析
library(lubridate)
out <- ymd_hms(data)
format(head(out), '%OS6')
#[1] "03.384309" "03.404017" "03.408064" "03.552361" "03.572690" "03.572690"
它解析毫秒,但是print
方法formats
的输出可以很好地在屏幕上打印。如果要查看毫秒数是否已经存在,
或与anytime
library(anytime)
anytime(data)
答案 1 :(得分:0)
签出asPOSIXct()
。要找出正确的format
参数,请查看?strptime
。
res <- as.POSIXct(data, format="%FD%R:%OS")
head(res, 3)
# [1] "2019-09-24 00:00:03.384309 CEST" "2019-09-24 00:00:03.404017 CEST"
# [3] "2019-09-24 00:00:03.408065 CEST"
class(res)
# [1] "POSIXct" "POSIXt"