来自R帮助功能:请注意,为了四舍五入,预计将使用IEC 60559标准,"转到偶数位".因此round(0.5)是0并且round(-1.5)是-2.
> round(0.5)
[1] 0
> round(1.5)
[1] 2
> round(2.5)
[1] 2
> round(3.5)
[1] 4
> round(4.5)
[1] 4
Run Code Online (Sandbox Code Playgroud)
但我需要将以.5结尾的所有值向下舍入.所有其他值应该舍入,因为它们由round()函数完成.例:
round(3.5) = 3
round(8.6) = 9
round(8.1) = 8
round(4.5) = 4
Run Code Online (Sandbox Code Playgroud)
有快速的方法吗?
krl*_*mlr 27
Per Dietrich Epp的评论,您可以使用ceiling()带偏移的函数来获得快速,矢量化,正确的解决方案:
round_down <- function(x) ceiling(x - 0.5)
round_down(seq(-2, 3, by = 0.5))
## [1] -2 -2 -1 -1 0 0 1 1 2 2 3
Run Code Online (Sandbox Code Playgroud)
我认为这比这里显示的许多其他解决方案更快,更简单.
正如Carl Witthoft所指出的,这为您的数据增加了比简单舍入更多的偏见.相比:
mean(round_down(seq(-2, 3, by = 0.5)))
## [1] 0.2727273
mean(round(seq(-2, 3, by = 0.5)))
## [1] 0.4545455
mean(seq(-2, 3, by = 0.5))
## [1] 0.5
Run Code Online (Sandbox Code Playgroud)
这种舍入程序的应用是什么?
Mar*_*zer 20
检查剩余部分x %% 1是否等于.5,然后将数字置于或舍入:
x <- seq(1, 3, 0.1)
ifelse(x %% 1 == 0.5, floor(x), round(x))
> 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3
Run Code Online (Sandbox Code Playgroud)
the*_*ail 14
我也会加入马戏团:
rndflr <- function(x) {
sel <- vapply(x - floor(x), function(y) isTRUE(all.equal(y, 0.5)), FUN.VALUE=logical(1))
x[sel] <- floor(x[sel])
x[!sel] <- round(x[!sel])
x
}
rndflr(c(3.5,8.6,8.1,4.5))
#[1] 3 9 8 4
Run Code Online (Sandbox Code Playgroud)
Jos*_*ien 10
此函数的工作原理是查找小数部分等于的元素 0.5,并在舍入前向它们添加一个小的负数,确保它们向下舍入.(它依赖于 - 无害但有点混淆 - 因为R中的布尔向量在乘以数字向量时将转换为0's和1's的向量.)
f <- function(x) {
round(x - .1*(x%%1 == .5))
}
x <- c(0.5,1,1.5,2,2.5,2.01,2.99)
f(x)
[1] 0 1 1 2 2 2 3
Run Code Online (Sandbox Code Playgroud)
函数(不是高尔夫球)非常简单,并检查剩下的小数是否为.5或小于.实际上,你可以轻松地使它更有用,并0.5作为一个参数:
nice.round <- function(x, myLimit = 0.5) {
bX <- x
intX <- as.integer(x)
decimals <- x%%intX
if(is.na(decimals)) {
decimals <- 0
}
if(decimals <= myLimit) {
x <- floor(x)
} else {
x <- round(x)
}
if (bX > 0.5 & bX < 1) {
x <- 1
}
return(x)
}
Run Code Online (Sandbox Code Playgroud)
目前,此功能无法正常使用0.5到1.0之间的值.
> nice.round(1.5)
[1] 1
> nice.round(1.6)
[1] 2
> nice.round(10000.624541)
[1] 10001
> nice.round(0.4)
[1] 0
> nice.round(0.6)
[1] 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2222 次 |
| 最近记录: |