gpt4 book ai didi

r - 如何避免在任何时间() "updates by reference"?

转载 作者:行者123 更新时间:2023-12-04 03:06:23 24 4
gpt4 key购买 nike

我想使用POSIXct将数字变量转换为anytime。我的问题是anytime(<numeric>)也会转换输入变量-我想保留它。

简单的例子:

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 R中 base的“不按引用更新”行为不同:
t_num <- 1529734500
as.POSIXct(t_num, origin = "1970-01-01")
# [1] "2018-06-23 08:15:00 CEST"
t_num
# 1529734500

同样, anydate(<numeric>)也通过引用进行更新:
d_num <- 17707
anydate(d_num)
# [1] "2018-06-25"
d_num
# [1] "2018-06-25"

我在 ?anytime中找不到对此行为的明确描述。我可以如上所述使用 as.POSIXct,但是有人知道如何在 anytime中处理吗?

最佳答案

您可以这样修改它:

library(anytime)
t_num <- 1529734500
anytime(t_num+0)
# POSIXct[1:1], format: "2018-06-23 08:15:00"
t_num
# [1] 1529734500

请注意,整数输入将被区别对待:
t_int <- 1529734500L
anytime(t_int)
# POSIXct[1:1], format: "2018-06-23 08:15:00"
t_int
# [1] 1529734500

关于r - 如何避免在任何时间(<numeric>) "updates by reference"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51010539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com