I want to convert a numeric variable to POSIXct
using anytime
. My issue is that anytime(<numeric>)
converts the input variable as well - I want to keep it.
Simple example:
library(anytime)
t_num <- 1529734500
anytime(t_num)
# [1] "2018-06-23 08:15:00 CEST"
t_num
# [1] "2018-06-23 08:15:00 CEST"
This differs from the 'non-update by reference' behaviour of as.POSIXct
in base
R:
t_num <- 1529734500
as.POSIXct(t_num, origin = "1970-01-01")
# [1] "2018-06-23 08:15:00 CEST"
t_num
# 1529734500
Similarly, anydate(<numeric>)
also updates by reference:
d_num <- 17707
anydate(d_num)
# [1] "2018-06-25"
d_num
# [1] "2018-06-25"
I can't find an explicit description of this behaviour in ?anytime
. I could use as.POSIXct
as above, but does anyone know how to handle this within anytime
?
t_num
is not really real (e.g.data.table:::isReallyReal(t_num)
), i.e., you can just replace it with1529734500L
:) – Hazardousclass(1529734500)
isnumeric
butclass(1529734500L)
is not, so you can eschew the1*
/0+
kludges by starting with an integer – Hazardousnumeric
, but as you point,data.table
reminds us we can express the same value using aninteger
. – Pharmaceutics