How to make time difference in same units when subtracting POSIXct
You can use difftime
for that propose which allows you to specify the measurement units, for example
difftime(t1, t2, units = "secs")
Another way (as mentioned by @nicola and is present in the same documentation) is to take advantage of the fact that -
has a -.POSIXt
method and override the measurement units after the subtraction operation using units<-
replacement method
res <- t1 - t2
units(res) <- "secs"
Related videos on Youtube
user3022875
Updated on July 09, 2022Comments
-
user3022875 almost 2 years
I want to subtract to POSIXct. I can do this but depending on the first row (i guess?) the difference will be in seconds or minutes. Below you can see the first diff is in seconds and the second diff is in minutes because I changed the time difference in the first row:
#diff in seconds because 1st row time diff is small? t1<- as.POSIXct(c("2015-02-02 20:18:03 00:00:00", "2015-02-02 20:17:02 00:00:00"),"GMT") t2<- as.POSIXct(c("2015-02-02 20:18:02 00:00:00","2015-02-02 20:18:02 00:00:00"),"GMT") d<-data.frame(t1= t1, t2= t2) d$t1-d$t2 #diff in seconds because 1st row time diff is larger? t1<- as.POSIXct(c("2015-02-02 20:13:03 00:00:00", "2015-02-02 20:17:02 00:00:00"),"GMT") t2<- as.POSIXct(c("2015-02-02 20:18:02 00:00:00","2015-02-02 20:18:02 00:00:00"),"GMT") d<-data.frame(t1= t1, t2= t2) d$t1-d$t2
results:
> #diff in seconds because 1st row time diff is small? > t1<- as.POSIXct(c("2015-02-02 20:18:03 00:00:00", "2015-02-02 20:17:02 00:00:00"),"GMT") > t2<- as.POSIXct(c("2015-02-02 20:18:02 00:00:00","2015-02-02 20:18:02 00:00:00"),"GMT") > d<-data.frame(t1= t1, t2= t2) > d$t1-d$t2 Time differences in secs [1] 1 -60 > > > #diff in seconds because 1st row time diff is larger? > t1<- as.POSIXct(c("2015-02-02 20:13:03 00:00:00", "2015-02-02 20:17:02 00:00:00"),"GMT") > t2<- as.POSIXct(c("2015-02-02 20:18:02 00:00:00","2015-02-02 20:18:02 00:00:00"),"GMT") > d<-data.frame(t1= t1, t2= t2) > d$t1-d$t2 Time differences in mins [1] -4.983333 -1.000000
I would like the difference to ALWAYS be in seconds no matter what the first row difference is. Is there a way to make this happen?
Thank you.
-
David Arenburg almost 9 years
difftime(t1, t2, units = "secs")
-
nicola almost 9 yearsOr, if you want to use
-
instead ofdifftime
, change the unit throughunits<-
. For instance:x<-d$t1-d$t2; units(x)<-"secs"
-
David Arenburg almost 9 years@user20650, Ok added. Hope nicola doesn't mind I blended his comment too.
-
-
HappyCoding about 7 yearsunits(res) <- "secs" seems not correct. it will also read 6 days as 6 seconds
-
David Arenburg about 7 years@HappyCoding Not sure what you mean.
res <- as.POSIXct("2015-05-02 20:18:02 00:00:00") - as.POSIXct("2015-02-02 20:18:02 00:00:00") ; units(res) <- "secs"
seem to work fine. -
HappyCoding about 7 yearsyes, above can work. My fault. seems it can only work with element instead of vector or data frame. my code example
> res <- as.POSIXct("2015-05-02 20:18:02 00:00:00") - as.POSIXct("2015-02-02 20:18:02 00:00:00") > res <- rbind(res,res) > as.numeric(res,units="secs")
-
David Arenburg about 7 years@HappyCoding That's because
rbind
caused it to lose itsdifftime
class andunits
only works with that class (as documented under?units
). This works on vectors as long they have the proper class. Tryres <- as.POSIXct(c("2015-05-02 20:18:02 00:00:00", "2015-05-02 20:18:02 00:00:00")) - as.POSIXct(c("2015-02-02 20:18:02 00:00:00", "2015-02-02 20:18:02 00:00:00")) ; units(res) <- "secs"
for instance. -
HappyCoding about 7 yearsthanks @David. Yes, you're right. I solved the problem with lappy to each instance.