从POSIXct提取时间 [英] Extracting time from POSIXct

查看:111
本文介绍了从POSIXct提取时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一系列丢弃日期部分的POSIXct对象中提取时间?



例如,我有:

  times<  -  structure(c(1331086009.50098,1331091427.42461,1331252565.99979,
1331252675.81601,1331262597.72474,1331262641.11786,1331269557.4059,
1331278779.26727,1331448476.96126,1331452596.13806 ),class = c(POSIXct,
POSIXt))

到这些日期:

 2012-03-07 03:06:49 CET2012-03-07 04:37 :07 CET
2012-03-09 01:22:45 CET2012-03-09 01:24:35 CET
2012-03-09 04:09:57 CET 2012-03-09 04:10:41 CET
2012-03-09 06:05:57 CET2012-03-09 08:39:39 CET
2012 -03-11 07:47:56 CET2012-03-11 08:56:36 CET

现在,我在这些时间测量的参数有一些值

  val < -  c(1.25343125e-05 ,0.00022890575,
3.9269125e-05,0.00022856818 75,
4.26353125e-05,5.982625e-05,
2.09575e-05,0.0001516951251,
2.653125e-05,0.001021391875)
/ pre>

我想绘制一天的瓦尔vs时间,而不考虑瓦特测量的具体日期。



是否有一个特定的功能可以让我这样做?

解决方案

您可以使用 strftime 将数据时间转换为任何字符格式:

 > t<  -  strftime(times,format =%H:%M:%S)
> t
[1]02:06:4903:37:0700:22:4500:24:3503:09:5703:10:41
[7]05:05:5707:39:3906:47:5607:56:36

但是,这并不是很有帮助,因为您要绘制数据。一个解决方法是从你的时代中删除date元素,然后在所有的时间添加相同的日期:

 > ; xx<  -  as.POSIXct(t,format =%H:%M:%S)
> xx
[1]2012-03-23 02:06:49 GMT2012-03-23 03:37:07 GMT
[3]2012-03-23 00:22 :45 GMT2012-03-23 00:24:35 GMT
[5]2012-03-23 03:09:57 GMT2012-03-23 03:10:41 GMT
[7]2012-03-23 05:05:57 GMT2012-03-23 07:39:39 GMT
[9]2012-03-23 06:47: 56 GMT2012-03-23 07:56:36 GMT

现在可以使用这些 datetime 您的情节中的对象:

  plot(xx,rnorm (xx)),xlab =时间,ylab =随机值)






有关更多帮助,请参阅?DateTimeClasses


How would I extract the time from a series of POSIXct objects discarding the date part?

For instance, I have:

times <- structure(c(1331086009.50098, 1331091427.42461, 1331252565.99979, 
1331252675.81601, 1331262597.72474, 1331262641.11786, 1331269557.4059, 
1331278779.26727, 1331448476.96126, 1331452596.13806), class = c("POSIXct", 
"POSIXt"))

which corresponds to these dates:

"2012-03-07 03:06:49 CET" "2012-03-07 04:37:07 CET" 
"2012-03-09 01:22:45 CET" "2012-03-09 01:24:35 CET" 
"2012-03-09 04:09:57 CET" "2012-03-09 04:10:41 CET"
"2012-03-09 06:05:57 CET" "2012-03-09 08:39:39 CET"
"2012-03-11 07:47:56 CET" "2012-03-11 08:56:36 CET"

Now, I have some values for a parameter measured at those times

val <- c(1.25343125e-05, 0.00022890575, 
         3.9269125e-05, 0.0002285681875, 
         4.26353125e-05, 5.982625e-05, 
         2.09575e-05, 0.0001516951251, 
         2.653125e-05, 0.0001021391875)

I would like to plot val vs time of the day, irrespectively of the specific day when val was measured.

Is there a specific function that would allow me to do that?

解决方案

You can use strftime to convert datetimes to any character format:

> t <- strftime(times, format="%H:%M:%S")
> t
 [1] "02:06:49" "03:37:07" "00:22:45" "00:24:35" "03:09:57" "03:10:41"
 [7] "05:05:57" "07:39:39" "06:47:56" "07:56:36"

But that doesn't help very much, since you want to plot your data. One workaround is to strip the date element from your times, and then to add an identical date to all of your times:

> xx <- as.POSIXct(t, format="%H:%M:%S")
> xx
 [1] "2012-03-23 02:06:49 GMT" "2012-03-23 03:37:07 GMT"
 [3] "2012-03-23 00:22:45 GMT" "2012-03-23 00:24:35 GMT"
 [5] "2012-03-23 03:09:57 GMT" "2012-03-23 03:10:41 GMT"
 [7] "2012-03-23 05:05:57 GMT" "2012-03-23 07:39:39 GMT"
 [9] "2012-03-23 06:47:56 GMT" "2012-03-23 07:56:36 GMT"

Now you can use these datetime objects in your plot:

plot(xx, rnorm(length(xx)), xlab="Time", ylab="Random value")


For more help, see ?DateTimeClasses

这篇关于从POSIXct提取时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆