in R, create a vector rounding function so that the coordinates are 100

3

I have a percentage vector, for example,

v<- c(32.5 , 43.2 , 24.1)
> round(v)
[1] 32 43 24
> sum(round(v))
[1] 99

I want a function that handles my rounding so that it gets 100. In other words, I want a function fun so that sum(fun(v))=100 . The criterion, of course, must be so that each coordinate of fun(v) gets as far from the original values as possible. Does anyone know of any method to do this?

    
asked by anonymous 13.02.2015 / 15:42

3 answers

2

I believe this is what you are looking for:

x1=32.5
x2=43.2
if (x1-trunc(x1)>=.5) {y1=trunc(x1)+1} else y1=trunc(x1)
if (x2-trunc(x2)>=.5) {y2=trunc(x2)+1} else y2=trunc(x2)
y3=100-(y1+y2)
v=c(y1,y2,y3)
v
sum(v)
    
06.03.2015 / 22:10
0

I do not believe there is an objective (non-subjective) way of doing this. Imagine the following scenario:

> v <- c(rep(10.1, 9), 9.1)
> sum(v)
[1] 100
> sum(round(v))
[1] 99

What value of the round(v) vector would you change to sum(round(v)) equal 100?

The best solution would be to use values without rounding. If it is not possible, based on what you think is reasonable, manually check which number should be changed or create a function that does this.

    
13.02.2015 / 16:48
0

Have you already looked at the floor and ceiling functions? I already needed that you're behind I did something like this:

> v<- c(32.5 , 43.2 , 24.1)
> v
[1] 32.5 43.2 24.1
> sum(v)
[1] 99.8
> ceiling(sum(v))
[1] 100

You can vary the application of the ceiling and use the floor in sum, there are several ways to do this. Even with round and other base rounding function of R:

ceiling(x)
floor(x)
trunc(x, ...)
round(x, digits = 0)
signif(x, digits = 6) 
    
25.02.2015 / 02:37