摘要 R 函数:舍入值
Summary R function: rounding values
我想知道是否有可能为这样的值更改汇总 r 函数的舍入方法:
> mean(c(1, 12,28,30, 34,25,35, 40))
[1] 25.625
> summary(c(1, 12,28,30, 34,25,35, 40))
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 21.75 29.00 25.62 34.25 40.00
在汇总函数中,我希望均值四舍五入为 25.63 而不是 25.62
祝你有愉快的一天!
您可以使用 here 中的 round2
函数:
round2 = function(x, n) {
posneg = sign(x)
z = abs(x)*10^n
z = z + 0.5 + sqrt(.Machine$double.eps)
z = trunc(z)
z = z/10^n
z*posneg
}
x <- c(1, 12,28,30, 34,25,35, 40)
round(mean(x), 2)
#[1] 25.62
round2(mean(x), 2)
#[1] 25.63
round(summary(x), 2)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1.00 21.75 29.00 25.62 34.25 40.00
round2(summary(x), 2)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1.00 21.75 29.00 25.63 34.25 40.00
我想知道是否有可能为这样的值更改汇总 r 函数的舍入方法:
> mean(c(1, 12,28,30, 34,25,35, 40))
[1] 25.625
> summary(c(1, 12,28,30, 34,25,35, 40))
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 21.75 29.00 25.62 34.25 40.00
在汇总函数中,我希望均值四舍五入为 25.63 而不是 25.62
祝你有愉快的一天!
您可以使用 here 中的 round2
函数:
round2 = function(x, n) {
posneg = sign(x)
z = abs(x)*10^n
z = z + 0.5 + sqrt(.Machine$double.eps)
z = trunc(z)
z = z/10^n
z*posneg
}
x <- c(1, 12,28,30, 34,25,35, 40)
round(mean(x), 2)
#[1] 25.62
round2(mean(x), 2)
#[1] 25.63
round(summary(x), 2)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1.00 21.75 29.00 25.62 34.25 40.00
round2(summary(x), 2)
# Min. 1st Qu. Median Mean 3rd Qu. Max.
# 1.00 21.75 29.00 25.63 34.25 40.00