R舍入到最接近的.5或.1 [英] R round to nearest .5 or .1

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

问题描述

我有一个股票价格数据集,它已经四舍五入到小数点后两位[c0].我现在正尝试四舍五入为每个股票不同的特定值.以下是一些示例:

I have a data set of stock prices that have already been rounded to 2 decimal places (1234.56). I am now trying to round to a specific value which is different for each stock. Here are some examples:

Current Stock Price         Minimum Tick Increment       Desired Output
  123.45                            .50                      123.50
  155.03                            .10                      155.00
  138.24                            .50                      138.00
  129.94                            .10                      129.90
   ...                              ...                       ...

我不确定如何做到这一点,但愿意提出建议.

I'm not really sure how to do this but am open to suggestions.

推荐答案

可能是

round(a/b)*b

将完成工作.

> a <- seq(.1,1,.13)
> b <- c(.1,.1,.1,.2,.3,.3,.7)
> data.frame(a, b, out = round(a/b)*b)
     a   b out
1 0.10 0.1 0.1
2 0.23 0.1 0.2
3 0.36 0.1 0.4
4 0.49 0.2 0.4
5 0.62 0.3 0.6
6 0.75 0.3 0.6
7 0.88 0.7 0.7

这篇关于R舍入到最接近的.5或.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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