R,四舍五入,天花板和地板 [英] R, rounding, ceiling and floors

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

问题描述

假设有一堆从 pnorm() 返回的数据,这样你就有了 .0003ish 和 .9999ish 之间的数字.

Suppose that one has a bunch of data returned from pnorm(), such that you've got numbers between .0003ish and .9999ish.

numbers     <- round(rnorm(n = 10000, mean = 100, sd = 15))
percentiles <- pnorm(numbers, mean = 100, sd = 15)*100

然后进一步假设有人对四舍五入的百分位数感兴趣,这样 .0003 或 whatevs 将变为 1(因此天花板()),但 99.999 将变为 99(因此地板()).

And then further suppose that one is interested in rounding the percentiles such that .0003 or whatevs will come out to 1 (so ceiling()), but 99.999 will come out to 99 (so floor()).

我想我正在寻找的是 round() ,它以某种方式巧妙地知道在极端情况下将其反转,但据我所知,不存在这样的事情.我是否必须用 if 语句来丑化它?有没有更好的方法来处理这样的事情?

I guess what I'm looking for is round() that somehow brilliantly knows to reverse it in the extreme cases, but as far as I know, no such thing exists. Am I going to have to ugly it up with an if statement? Is there a better method of handling such a thing?

推荐答案

您可以使用 round 并强制将东西放入 199 在使用 pminpmax 的四肢:

You could use round and force things into 1 or 99 at the extremities using pmin and pmax:

pmax(1, pmin(99, round(percentiles)))

这篇关于R,四舍五入,天花板和地板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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