如何避免任何时间(<数字>)“通过引用更新”? [英] How to avoid that anytime(<numeric>) "updates by reference"?

查看:51
本文介绍了如何避免任何时间(<数字>)“通过引用更新”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用随时将数值变量转换为 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

这篇关于如何避免任何时间(&lt;数字&gt;)“通过引用更新”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆