-4

Say I have a vector of integers: tt <- c(26000L, 26000L, 25333L, 25333L, 25343L, 24667L, 24667L, 24667L, 23667L)

I want to replace these integers within the interval of 500 and get:

26000 26000 25500 25500 25500 25000 25000 25000 24000

MAPK
  • 5,635
  • 4
  • 37
  • 88

2 Answers2

2

You can use round_any from the plyr library.

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
sumshyftw
  • 1,111
  • 6
  • 14
1

I'm not sure about your rounding logic, but what about

round(as.numeric(tt) / 500) * 500
# [1] 26000 26000 25500 25500 25500 24500 24500 24500 23500
jay.sf
  • 60,139
  • 8
  • 53
  • 110