2013-04-30 24 views
1

如何從POSIXlt對象中提取小數? as.numeric()只給出自紀元以來的秒數。如何從POSIXlt中提取索引的第二部分?

as.character((as.POSIXlt(1366736969, origin="1970-01-01") + 0.001172)) 
[1] "2013-04-23 13:09:29.001172" 
as.numeric((as.POSIXlt(1366736969, origin="1970-01-01") + 0.001172)) 
[1] 1366736969 
+7

不,它不需要,但是這一切都打印:'as.numeric((as.POSIXlt(1366736969 ,origin =「1970-01-01」)+ 0.001172))%% 1' – 2013-04-30 16:27:44

+0

@JoshuaUlrich聽起來像是一個答案。 – 2013-04-30 18:22:44

回答

3

as.numeric不僅返回秒,這就是它如何打印。你可以通過提高精度來看到這一點。

options(digits.secs=6) 
p <- as.POSIXct(1366736969, origin="1970-01-01") + 0.001172 
as.numeric(p) 
# [1] 1366758569 
od <- options("digits") 
options(digits=22) 
as.numeric(p) 
# [1] 1366758569.001172065735 

如果你想提取子秒,只需使用trunc

options(od) 
p-trunc(p) 
# Time difference of 0.001172066 secs 
+1

'p %% 1'也適用於此。 (編輯 - 我看到你在上面的評論中有這個!) – 2013-04-30 19:55:12

相關問題