在 R 中将日期转换为纳秒
Convert a Date to nanoseconds in R
我正在尝试在 R 中将日期转换为纳秒。我尝试使用 lubridate 包但没有成功。
dateval <- c("2016-06-29")
感谢任何帮助!
谢谢。
设置:
require(lubridate)
dateval <- c("2016-06-29")
dateeval <- ymd(dateval)
纳秒是 SI 时间单位,等于十亿分之一秒。
纪元与该日期之间的时间差(以纳秒为单位)(近似值,因为没有带日期的时间戳):
difftime(dateeval, Sys.time(), units = "secs")
abs(as.numeric(difftime(ymd("1970-01-01"),dateeval, units = "secs")))*1000000000
[1] 1.467158e+18
使用microbenchmark
包。程序包 returns 以精确执行某项操作所需的时间为基准。在包内,它们有一个函数,可以 return 以纳秒为单位的当前时间
microbenchmark::get_nanotime()
[1] 3.610932e+14
microbenchmark::get_nanotime()
[1] 3.610952e+14
microbenchmark::get_nanotime()
[1] 3.610958e+14
microbenchmark::get_nanotime()
[1] 3.610963e+14
或者,如果精度变得不那么重要,您可以使用
as.numeric(as.POSIXct(TIMEA)) - as.numeric(as.POSIXct(TIMEB))
我正在尝试在 R 中将日期转换为纳秒。我尝试使用 lubridate 包但没有成功。
dateval <- c("2016-06-29")
感谢任何帮助!
谢谢。
设置:
require(lubridate)
dateval <- c("2016-06-29")
dateeval <- ymd(dateval)
纳秒是 SI 时间单位,等于十亿分之一秒。
纪元与该日期之间的时间差(以纳秒为单位)(近似值,因为没有带日期的时间戳):
difftime(dateeval, Sys.time(), units = "secs")
abs(as.numeric(difftime(ymd("1970-01-01"),dateeval, units = "secs")))*1000000000
[1] 1.467158e+18
使用microbenchmark
包。程序包 returns 以精确执行某项操作所需的时间为基准。在包内,它们有一个函数,可以 return 以纳秒为单位的当前时间
microbenchmark::get_nanotime()
[1] 3.610932e+14
microbenchmark::get_nanotime()
[1] 3.610952e+14
microbenchmark::get_nanotime()
[1] 3.610958e+14
microbenchmark::get_nanotime()
[1] 3.610963e+14
或者,如果精度变得不那么重要,您可以使用
as.numeric(as.POSIXct(TIMEA)) - as.numeric(as.POSIXct(TIMEB))