【问题标题】:How to round integer by intervals of 500 in R [duplicate]如何在R中以500的间隔舍入整数[重复]
【发布时间】:2019-07-08 17:47:53
【问题描述】:

假设我有一个整数向量:tt <- c(26000L, 26000L, 25333L, 25333L, 25343L, 24667L, 24667L, 24667L, 23667L)

我想在500的区间内替换这些整数,得到:

26000 26000 25500 25500 25500 25000 25000 25000 24000

【问题讨论】:

标签: r


【解决方案1】:

您可以使用plyr 库中的round_any

library(plyr)

round_any(as.numeric(tt), 500)
[1] 26000 26000 25500 25500 25500 24500 24500 24500 23500

round_any(as.numeric(tt), 500, f = floor)
[1] 26000 26000 25000 25000 25000 24500 24500 24500 23500

round_any(as.numeric(tt), 500, f = ceiling)
[1] 26000 26000 25500 25500 25500 25000 25000 25000 24000

【讨论】:

    【解决方案2】:

    我不确定你的舍入逻辑,但是呢

    round(as.numeric(tt) / 500) * 500
    # [1] 26000 26000 25500 25500 25500 24500 24500 24500 23500
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-21
      • 2011-12-14
      相关资源
      最近更新 更多