Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Control POSIX time difference output in R

Tags:

datetime

r

In R I have two strings:

t <- c("2010-01-01 00:01:02", "2010-01-01 00:02:02")

which I convert to POSIX datetimes:

dt <- as.POSIXct(t)

Taking the difference of the two dt[2] - dt[1] gives:

Time difference of 1 mins

Cool. But how do I force the time difference to be in, say, seconds?

like image 806
Andy Barbour Avatar asked Sep 20 '11 21:09

Andy Barbour


People also ask

How do you find the difference between two time in R?

You can use the difftime() function to calculate the time difference between two dates or datetimes in R. where: time1, time2: The two dates or datetimes. units: The units to use for time difference (default is “days”, but other options include “secs”, “mins”, “hours”, and “weeks”)

How do you deal with time in R?

R provides several options for dealing with date and date/time data. The builtin as. Date function handles dates (without times); the contributed library chron handles dates and times, but does not control for time zones; and the POSIXct and POSIXlt classes allow for dates and times with control for time zones.

What is the difference between POSIXct and POSIXlt?

First, there are two internal implementations of date/time: POSIXct , which stores seconds since UNIX epoch (+some other data), and POSIXlt , which stores a list of day, month, year, hour, minute, second, etc. strptime is a function to directly convert character vectors (of a variety of formats) to POSIXlt format.


1 Answers

> difftime(dt[2], dt[1], units="secs")
Time difference of 60 secs
like image 56
IRTFM Avatar answered Nov 15 '22 09:11

IRTFM