Retrieving time from POSIXct

How do I extract time from a series of POSIXct objects that discard part of a date?

For example, 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 matches 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 the parameter measured at this time

 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 build val vs time of day, no matter what day val was measured.

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

+55
date time r posixct
Mar 23 2018-12-12T00:
source share
5 answers

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 it doesn’t help much, because you want to print your data. One way is to remove the date element from your time and then add the identical date at all 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") 

enter image description here




See ?DateTimeClasses more details.

+70
Mar 23 2018-12-12T00:
source share

There were previous answers that showed a trick. Essentially:

  • you must save the POSIXct types to take advantage of all existing POSIXct functions

  • if you want to "superimpose" a few days on one plot, highlighting the intraday variation, the best trick too ...

  • appoint the same day (and a month and even a year, if necessary, what is wrong here)

which you can do by overriding the components of the day and month in the POSIXlt or simply shifting the "delta" relative to 0:00:00 between different days.

So, using times and val , as provided to you:

 ## impose month and day based on first obs ntimes <- as.POSIXlt(times) # convert to 'POSIX list type' ntimes$mday <- ntimes[1]$mday # and $mon if it differs too ntimes <- as.POSIXct(ntimes) # convert back par(mfrow=c(2,1)) plot(times,val) # old times plot(ntimes,val) # new times 

gives this contrast to the original and modified timelines:

enter image description here

+12
Mar 23 2018-12-12T00:
source share

I cannot find anything that deals with the clock, exactly, so I would just use some of the functions from the package: lubridate and work with seconds from midnight:

 require(lubridate) clockS = function(t){hour(t)*3600+minute(t)*60+second(t)} plot(clockS(times),val) 

Then you can take a look at some axis code to understand how to mark the axes well.

+3
Mar 23 2018-12-12T00:
source share

Many solutions were provided, but I did not see this which uses the chron package:

 hours = times(strftime(times, format="%T")) plot(val~hours) 

(Sorry, I do not have the right to post the image, you will have to capture it yourself)

+3
May 6 '15 at
source share

The time_t value for midnight GMT is always divided by 86400 ( 24 * 3600 ). Thus, the seconds are from midnight GMT time %% 86400 .

The hour is GMT (time %% 86400) / 3600 , and this can be used as the x axis of the graph:

 plot((as.numeric(times) %% 86400)/3600, val) 

enter image description here

To adjust the time zone, adjust the time before accepting the module by adding the number of seconds during which the time zone exceeds GMT. For example, U.S. Central Daylight Saving Time (CDT) is 5 hours behind GMT. To plot against time, the following expression is used in the CDT:

 plot(((as.numeric(times) - 5*3600) %% 86400)/3600, val) 
+2
May 27 '14 at 2:47
source share



All Articles