舍入毫秒的R问题 [英] R issue with rounding milliseconds

查看:132
本文介绍了舍入毫秒的R问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于以下问题,在R下将毫秒取整.如何解决这个问题,以确保时间正确?

Given the following issue with rounding milliseconds under R. How do I get around it so that the times are correct?

> options(digits.secs=3)
> as.POSIXlt("13:29:56.061", format='%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.060 UTC"
> as.POSIXlt("13:29:56.062", format='%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.061 UTC"
> as.POSIXlt("13:29:56.063", format='%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.063 UTC"

我注意到此URL提供了背景信息,但不能解决我的问题: 在R中调用strptime时的毫秒级困惑.

I noticed that this URL provides background information but doesn't solve my issue: Milliseconds puzzle when calling strptime in R.

此URL也涉及该问题,但不能解决: R xts:索引为.001毫秒.

Also this URL touches on the issue but doesn't solve it: R xts: .001 millisecond in index.

在这些情况下,我确实看到了以下内容:

In these cases I do see the following:

> x <- as.POSIXlt("13:29:56.061", format='%H:%M:%OS', tz='UTC')
> print(as.numeric(x), digits=20)
[1] 1339075796.0610001087

URL似乎也表明这只是一个显示问题,但我注意到使用类似"%OS3"的语句而没有选项行似乎并没有获得正确的数字.

The URL also seems to indicate that this is just a display issue but I've noticed that using statements like "%OS3" without the options line don't seem to pickup the correct number of digits.

我正在使用的版本在Windows下是32位2.15.0,但是在R的其他情况下似乎存在.

The version I'm using is 32 bit 2.15.0 under Windows but this seems to exist under other situations for R.

请注意,我的原始数据是CSV文件中的这些日期时间字符串,我必须找到一种将其从字符串转换为正确的毫秒时间的方法.

Note that my original data is these date time strings within a CSV file I must find a way of converting them into the correct millisecond time from a string.

推荐答案

我看不到:

> options(digits.secs = 4)
> as.POSIXlt("13:29:56.061", format = '%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.061 UTC"
> as.POSIXlt("13:29:56.062", format = '%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.062 UTC"
> as.POSIXlt("13:29:56.063", format = '%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.063 UTC"
> options(digits.secs = 3)
> as.POSIXlt("13:29:56.061", format = '%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.061 UTC"
> as.POSIXlt("13:29:56.062", format = '%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.062 UTC"
> as.POSIXlt("13:29:56.063", format = '%H:%M:%OS', tz='UTC')
[1] "2012-06-07 13:29:56.063 UTC"

使用

> sessionInfo()
R version 2.15.0 Patched (2012-04-14 r59019)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
 [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
 [5] LC_MONETARY=en_GB.utf8    LC_MESSAGES=en_GB.utf8   
 [7] LC_PAPER=C                LC_NAME=C                
 [9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base

使用"%OSn"格式字符串,将强制截断.如果小数秒不能精确地用浮点数表示,则截断很可能会以错误的方式进行.如果您发现事情出了问题,还可以将其显式舍入为所需的单位,或者添加您想要操作的分数的一半(在显示0.0005的情况下):

With the "%OSn" format strings, one forces truncation. If the fractional second cannot be represented exactly in floating points then the truncation may very well go the wrong way. If you see things going to wrong way you can also round explicitly to the unit you want or add a half of the fraction you wish to operate at (in the case shown 0.0005):

> t1 <- as.POSIXlt("13:29:56.061", format = '%H:%M:%OS', tz='UTC')
> t1
[1] "2012-06-07 13:29:56.061 UTC"
> t1 + 0.0005
[1] "2012-06-07 13:29:56.061 UTC"

(但是我说过,我在这里看不到问题.)

(but a I said, I don't see the problem here.)

后一点是由R-上的 Simon Urbanek提出的. Devel邮件列表,日期为2012年5月30日.

这篇关于舍入毫秒的R问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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