I have multiple lists of measurements. In each list have the timestramp formated as a string ("2009-12-24 21:00:07.0") and I know that each measurement in the list is separated by 5 seconds. I want to combine all data into a huge data.frame in R. Afterwards I want to be able to easily access the time difference of two measurements so I probably should convert data into something different than characters.
Which format should I use to store the times? Is there some time format in some package that I should use?
To get current time in R, call Sys. time() function. Sys. time() returns absolute date-time value with an accuracy of sub-second.
The default for the format methods is "%Y-%m-%d %H:%M:%S" if any element has a time component which is not midnight, and "%Y-%m-%d" otherwise. If options("digits. secs") is set, up to the specified number of digits will be printed for seconds.
as. POSIXct stores both a date and time with an associated time zone. The default time zone selected, is the time zone that your computer is set to which is most often your local time zone. POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970.
You want the (standard) POSIXt
type from base R that can be had in 'compact form' as a POSIXct
(which is essentially a double representing fractional seconds since the epoch) or as long form in POSIXlt
(which contains sub-elements). The cool thing is that arithmetic etc are defined on this -- see help(DateTimeClasses)
Quick example:
R> now <- Sys.time() R> now [1] "2009-12-25 18:39:11 CST" R> as.numeric(now) [1] 1.262e+09 R> now + 10 # adds 10 seconds [1] "2009-12-25 18:39:21 CST" R> as.POSIXlt(now) [1] "2009-12-25 18:39:11 CST" R> str(as.POSIXlt(now)) POSIXlt[1:9], format: "2009-12-25 18:39:11" R> unclass(as.POSIXlt(now)) $sec [1] 11.79 $min [1] 39 $hour [1] 18 $mday [1] 25 $mon [1] 11 $year [1] 109 $wday [1] 5 $yday [1] 358 $isdst [1] 0 attr(,"tzone") [1] "America/Chicago" "CST" "CDT" R>
As for reading them in, see help(strptime)
As for difference, easy too:
R> Jan1 <- strptime("2009-01-01 00:00:00", "%Y-%m-%d %H:%M:%S") R> difftime(now, Jan1, unit="week") Time difference of 51.25 weeks R>
Lastly, the zoo package is an extremely versatile and well-documented container for matrix with associated date/time indices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With