Time differences¶
# It does not matter whether we have "POSIXct" or "POSIXlt" objects,
# we can always obtain differences between the tiems
t0 <- as.POSIXlt(0,origin="2020-02-01",tz="GMT")
t1 <- as.POSIXct(0,origin="2020-02-01 3:00",tz="GMT")
t2 <- as.POSIXlt(0,origin="2020-02-01 3:45",tz="GMT")
t3 <- as.POSIXct(0,origin="2020-02-01 3:45:06",tz="GMT")
# The unit of measurement for time differences is selected
# automatically. Usually it is the largest sensible unit:
t1 - t0
Time difference of 3 hours
t2 - t1
Time difference of 45 mins
t3 - t2
Time difference of 6 secs
t3 - t0
Time difference of 3.751667 hours
# The last difference is in hours and hour fractions. It might be more sensible
# to have seconds as units of measuremnt.
diff.t <- t3 - t0
units(diff.t) <- "secs"
diff.t
Time difference of 13506 secs
# It is also possible to compute differences between dates:
d0 <- as.Date("2020-01-31")
d1 <- as.Date("2020-02-28")
d2 <- as.Date("2020-03-31")
# Usually the difference is in days:
d1 - d0
Time difference of 28 days
d2 - d0
Time difference of 60 days
# We may also want to see the difference in hours:
diff.d <- d1 - d0
units(diff.d) <- "hours"
diff.d
Time difference of 672 hours
# It is also possible to create time durations from scratch
# From strings:
as.difftime("0:30:00")
Time difference of 30 mins
# and from numbers, here it is necessary to specify the unit of measurement
as.difftime(30, units="mins")
Time difference of 30 mins
- R file: time-differences.R
- Rmarkdown file: time-differences.Rmd
- Jupyter notebook file: time-differences.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):