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.
我想随时使用数字变量转换为POSIXct。我的问题是,任何时候(
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:
这与基础R中as.POSIXct的“非按引用更新”行为不同:
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:
同样,anydate(
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
?
我无法在任何时间找到这种行为的明确描述。我可以像上面那样使用as.POSIXct,但是有人知道如何在任何时候处理这个吗?
5 个解决方案
#1
2
You could hack it like this:
你可以像这样破解它:
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
#2
10
anytime
author here: this is standard R and Rcpp and passing-by-SEXP
behaviour: you cannot protect a SEXP
being passed from being changed.
任何时候作者:这是标准的R和Rcpp以及路过SEXP的行为:你无法保护被传递的SEXP被改变。
The view that anytime
takes is that you are asking for an input to be converted to a POSIXct
as that is what anytime
does: from char, from int, from factor, from anything. As a POSIXct
really is a numeric value (plus a S3 class attribute) this is what you are getting.
任何时候都需要的视图是你要求输入转换为POSIXct,因为这是任何时候的:从char,从int,从factor,从任何东西。由于POSIXct确实是一个数值(加上一个S3类属性),这就是你得到的。
If you do not want this (counter to the design of anytime
) you can do what @Moody_Mudskipper and @PKumar showed: used a temporary expression (or variable).
如果您不想这样(与任何时候的设计相反),您可以执行@Moody_Mudskipper和@PKumar所显示的内容:使用临时表达式(或变量)。
(I also think the data.table
example is a little unfair as data.table
-- just like Rcpp
-- is very explicit about taking references where it can. So of course it refers back to the original variable. There are idioms for deep copy if you need them.)
(我也认为data.table示例有点不公平,因为data.table - 就像Rcpp一样 - 非常明确地指出它可以在哪里引用。所以当然它引用了原始变量。有深刻的成语如果你需要,请复制。)
Lastly, an obvious trick is to use format
if you just want different display:
最后,一个明显的技巧是使用格式,如果你只是想要不同的显示:
R> d <- data.frame(t_num=1529734500)
R> d[1, "posixct"] <- format(anytime::anytime(d[1, "t_num"]))
R> d
t_num posixct
1 1529734500 2018-06-23 01:15:00
R>
That would work the same way in data.table
, of course, as the string representation is a type change. Ditto for IDate
/ ITime
.
当然,这将在data.table中以相同的方式工作,因为字符串表示是类型更改。同上IDate / ITime。
Edit: And the development version in the Github repo has had functionality to preserve the incoming argument since June 2017. So the next CRAN version, whenever I will push it, will have it too.
编辑:自2017年6月以来,Github repo中的开发版本具有保留传入参数的功能。因此,下一个CRAN版本,无论何时推送它,都会拥有它。
#3
2
If you do this, it will work :
如果你这样做,它将工作:
t_num <- 1529734500
anytime(t_num*1)
#> anytime(t_num*1)
#[1] "2018-06-23 06:15:00 UTC"
#> t_num
#[1] 1529734500
#4
2
Any reason to be married to anytime
?
任何时候结婚的理由?
.POSIXct(t_num, tz = 'Europe/Berlin')
# [1] "2018-06-23 08:15:00 CEST"
.POSIXct(x, tz)
is a wrapper for structure(x, class = c('POSIXct', 'POSIXt'), tzone = tz)
(i.e. you can ignore declaring the origin), and is essentially as.POSIXct.numeric
(except the latter is flexible in allowing non-UTC origin dates), look at print(as.POSIXct.numeric)
.
.POSIXct(x,tz)是结构的包装器(x,class = c('POSIXct','POSIXt'),tzone = tz)(即你可以忽略声明原点),并且基本上是as.POSIXct.numeric (除了后者允许非UTC原始日期灵活),请查看print(as.POSIXct.numeric)。
#5
2
When I did my homework before posting the question, I checked the open anytime
issues. I have now browsed the closed ones as well, where I found exactly the same issue as mine:
当我在发布问题之前完成我的作业时,我随时检查了问题。我现在也浏览了封闭的那些,在那里我找到了和我一样的问题:
随时覆盖输入
There the package author writes:
包裹作者写道:
I presume because
as.POSIXct()
leaves its input alone, we should too?我认为因为as.POSIXct()单独留下输入,我们也应该这样做?
So from anytime version 0.3.1 (unreleased)
:
所以从任何时候版本0.3.1(未发布):
Numeric input is now preserved rather than silently cast to the return object type
现在保留数字输入而不是静默转换为返回对象类型
Thus, one answer to my question is: "wait for 0.3.1
"*.
因此,我的问题的一个答案是:“等待0.3.1”*。
When 0.3.1
is released, the behaviour of anytime(<numeric>)
will agree with anytime(<non-numeric>)
and as.POSIXct(<numeric>)
, and work-arounds not needed.
当0.3.1被释放时,任何时候(
*Didn't have to wait too long: 0.3.1 is now released: "Numeric input is now preserved rather than silently cast to the return object type"
*不必等待太久:0.3.1现在已经发布:“现在保留数字输入而不是静默地转换为返回对象类型”
#1
2
You could hack it like this:
你可以像这样破解它:
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
#2
10
anytime
author here: this is standard R and Rcpp and passing-by-SEXP
behaviour: you cannot protect a SEXP
being passed from being changed.
任何时候作者:这是标准的R和Rcpp以及路过SEXP的行为:你无法保护被传递的SEXP被改变。
The view that anytime
takes is that you are asking for an input to be converted to a POSIXct
as that is what anytime
does: from char, from int, from factor, from anything. As a POSIXct
really is a numeric value (plus a S3 class attribute) this is what you are getting.
任何时候都需要的视图是你要求输入转换为POSIXct,因为这是任何时候的:从char,从int,从factor,从任何东西。由于POSIXct确实是一个数值(加上一个S3类属性),这就是你得到的。
If you do not want this (counter to the design of anytime
) you can do what @Moody_Mudskipper and @PKumar showed: used a temporary expression (or variable).
如果您不想这样(与任何时候的设计相反),您可以执行@Moody_Mudskipper和@PKumar所显示的内容:使用临时表达式(或变量)。
(I also think the data.table
example is a little unfair as data.table
-- just like Rcpp
-- is very explicit about taking references where it can. So of course it refers back to the original variable. There are idioms for deep copy if you need them.)
(我也认为data.table示例有点不公平,因为data.table - 就像Rcpp一样 - 非常明确地指出它可以在哪里引用。所以当然它引用了原始变量。有深刻的成语如果你需要,请复制。)
Lastly, an obvious trick is to use format
if you just want different display:
最后,一个明显的技巧是使用格式,如果你只是想要不同的显示:
R> d <- data.frame(t_num=1529734500)
R> d[1, "posixct"] <- format(anytime::anytime(d[1, "t_num"]))
R> d
t_num posixct
1 1529734500 2018-06-23 01:15:00
R>
That would work the same way in data.table
, of course, as the string representation is a type change. Ditto for IDate
/ ITime
.
当然,这将在data.table中以相同的方式工作,因为字符串表示是类型更改。同上IDate / ITime。
Edit: And the development version in the Github repo has had functionality to preserve the incoming argument since June 2017. So the next CRAN version, whenever I will push it, will have it too.
编辑:自2017年6月以来,Github repo中的开发版本具有保留传入参数的功能。因此,下一个CRAN版本,无论何时推送它,都会拥有它。
#3
2
If you do this, it will work :
如果你这样做,它将工作:
t_num <- 1529734500
anytime(t_num*1)
#> anytime(t_num*1)
#[1] "2018-06-23 06:15:00 UTC"
#> t_num
#[1] 1529734500
#4
2
Any reason to be married to anytime
?
任何时候结婚的理由?
.POSIXct(t_num, tz = 'Europe/Berlin')
# [1] "2018-06-23 08:15:00 CEST"
.POSIXct(x, tz)
is a wrapper for structure(x, class = c('POSIXct', 'POSIXt'), tzone = tz)
(i.e. you can ignore declaring the origin), and is essentially as.POSIXct.numeric
(except the latter is flexible in allowing non-UTC origin dates), look at print(as.POSIXct.numeric)
.
.POSIXct(x,tz)是结构的包装器(x,class = c('POSIXct','POSIXt'),tzone = tz)(即你可以忽略声明原点),并且基本上是as.POSIXct.numeric (除了后者允许非UTC原始日期灵活),请查看print(as.POSIXct.numeric)。
#5
2
When I did my homework before posting the question, I checked the open anytime
issues. I have now browsed the closed ones as well, where I found exactly the same issue as mine:
当我在发布问题之前完成我的作业时,我随时检查了问题。我现在也浏览了封闭的那些,在那里我找到了和我一样的问题:
随时覆盖输入
There the package author writes:
包裹作者写道:
I presume because
as.POSIXct()
leaves its input alone, we should too?我认为因为as.POSIXct()单独留下输入,我们也应该这样做?
So from anytime version 0.3.1 (unreleased)
:
所以从任何时候版本0.3.1(未发布):
Numeric input is now preserved rather than silently cast to the return object type
现在保留数字输入而不是静默转换为返回对象类型
Thus, one answer to my question is: "wait for 0.3.1
"*.
因此,我的问题的一个答案是:“等待0.3.1”*。
When 0.3.1
is released, the behaviour of anytime(<numeric>)
will agree with anytime(<non-numeric>)
and as.POSIXct(<numeric>)
, and work-arounds not needed.
当0.3.1被释放时,任何时候(
*Didn't have to wait too long: 0.3.1 is now released: "Numeric input is now preserved rather than silently cast to the return object type"
*不必等待太久:0.3.1现在已经发布:“现在保留数字输入而不是静默地转换为返回对象类型”