How can I fix weird rounding/truncation behavior of as.integer() in R? [duplicate]
I have a data frame with a column that contains percentage values in double format which I want to transform to an integer format. For example, 33% is currently represented as 0.33 and I want to change this to 33.
I have built a helper function that looks like this:
transform <- function(x) {
x <- x * 100
as.integer(x)
}
Now, if I run this, my rows with the value 0.33 get turned into 32 instead of 33. I have separated the multiplication to a line before as.integer(x)
, which correctly transforms my values to 33 (if I comment out as.integer(x)
). But if I run the whole function, the values get turned into 32.
Any ideas why this might happen and how to fix it? To my understanding, this has nothing to do with truncation of digits behind the dot, because the values are already correctly transformed to 33.
Solution 1:
Note 1: as @jay.sf pointed out in the comments, transform()
is already a pre-existing function in base
R, and it's not great practice to overwrite it. I recommend changing the name of your helper function and adjusting all the calls to it.
I suspect the underlying culprit is actually those latter digits; I recommend explicitly round()
ing, i.e.
my_transform <- function(x){
x <- round(x * 100, 0)
as.integer(x)
}
so that you're not relying on as.integer()
to do something it's not intended to do (rounding, in this case).