Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - round to nearest half [duplicate]

I have the following data:

> vec
 [1] 0.0 0.5 1.0 1.4 1.9 2.4 3.1 3.6 4.1 4.6 5.0 5.5 6.0 6.5 7.0 7.4 7.9 8.4 9.1 

which I need to round to the nearest .5.

Let me be more specific: 1.4 becomes 1.5, and 1.9 becomes 2.0. Also, 2.4 becomes 2.5, and 3.1 becomes 3.0. And so on. The vector I expect is:

> vec
 [1] 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 

Any ideas?

Many thanks.

like image 420
thiagoveloso Avatar asked Sep 30 '22 04:09

thiagoveloso


1 Answers

Don't know what language you're using, but the math would be NUM = INTEGER (NUM x 2) / 2

like image 51
Abe Gold Avatar answered Oct 03 '22 04:10

Abe Gold