2013-06-25 158 views
2

的正确用法我有一个在下面的格式命名factortimings与时间戳列:R:时间戳,Unix时间和“strptime”

1/11/07 15:15 

我申请上timings strptime产生tStamp为如下:

tStamp=strptime(timings,format="%m/%d/%Y %H:%M") 

I) 在TSTAMP相应的条目看起来像0007-01-11 15:15:00现在。为什么它会将200707分成0007?什么是生成tStamp的正确方法?

ii) 正确生成tStamp后,我们如何将它转换为Unix时间秒。 (自1970年以来的秒数)格式?

回答

4

需要2位数的年小写%y

R> pt <- strptime("1/11/07 15:15",format="%m/%d/%y %H:%M") 
R> pt 
[1] "2007-01-11 15:15:00 CST" 
R> 

其中CST是我的本地时区。

而且as.numeric()as.double()转换为双...

R> as.numeric(pt) 
[1] 1168550100 

...其中有零点几秒,如果这些是输入:

R> options("digits.secs"=3) # show milliseconds 
R> as.numeric(Sys.time())  # convert current time 
[1] 1372201674.52    # now with sub0seconds. 
+0

感谢您的编辑,@sgibb。我将再次展开以显示亚秒。 –