Home > OS >  Round off values in a datatable column to nearest 50
Round off values in a datatable column to nearest 50

Time:11-11

I know of round() in R, which is able to round off values by passing the number of digits to limit the value to. However, I wish to round up exact values(e.g. 1832-->1850, 1379-->1400) I get to the nearest 50, as shown in the Premium column below.

enter image description here

Is there a function to do this? Thank you!

CodePudding user response:

You may try

x <- c(102, 100, 405, 1187)
round(x/50)* 50

[1]  100  100  400 1200

CodePudding user response:

It might be easier to just use:

library(plyr)
round_any(x, 50)

round(1832, 50)
[1] 1850
  •  Tags:  
  • r
  • Related