如何避免任何时间(<数字>)“通过引用更新”? [英] How to avoid that anytime(<numeric>) "updates by reference"?
问题描述
我想使用随时
将数值变量转换为 POSIXct
。我的问题是 anytime(< numeric>)
也会转换输入变量-我想保留它。
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.
简单示例:
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"
这不同于 as.POSIXct的未按引用更新行为在
基中的code>
R:
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
类似地, anydate(<数字>)
也通过引用进行更新:
Similarly, anydate(<numeric>)
also updates by reference:
d_num <- 17707
anydate(d_num)
# [1] "2018-06-25"
d_num
# [1] "2018-06-25"
我在中找不到对此行为的明确描述any ime
。我可以像上面那样使用 as.POSIXct
,但是有人知道如何在 内随时处理吗?
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
?
推荐答案
您可以像这样破解它:
library(anytime)
t_num <- 1529734500
anytime(t_num+0)
# POSIXct[1:1], format: "2018-06-23 08:15:00"
t_num
# [1] 1529734500
整数输入将被不同地对待:
Note that an integer input will be treated differently:
t_int <- 1529734500L
anytime(t_int)
# POSIXct[1:1], format: "2018-06-23 08:15:00"
t_int
# [1] 1529734500
这篇关于如何避免任何时间(<数字>)“通过引用更新”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!