Truncation of values in R

Asked

Viewed 201 times

3

I need to do a survey to detect how many employees currently earn equal to or above the INSS ceiling (R$ 5531.31, in the base reference month). The databases I have inform the contribution values of the server (11%) and the employer (22%), but not the total remuneration. So an employee who gains more or equal to the tact he contributes the same value, because the maximum of the calculation base is the said ceiling. The problem is that 0.22 * 5531.31 = 1216.888, but the bases that I have simply ignore the last number. An employee who earns more or equal to the ceiling has as a contribution R$ 1216.88 and R can’t detect who wins more (or equal), as it rounds the multiplication result to 1216.89. Is there a function that ignores the last digits without it doing the rounding?

1 answer

5


I don’t know any native R function that does this, but I quickly created a function that I believe will do what you want:

arredondamento <- function(x){
  floor(100*x)/100
}

I used your example on her and gave the expected result:

> arredondamento(0.22 * 5531.31)
[1] 1216.88

This function can even be applied in vectors:

valores <- c(12.567, 31.333, 0.771)
arredondamento(valores)
[1] 12.56 31.33  0.77
  • 3

    Very good, very simple solution in style: "Because I didn’t think of it before?!"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.