How to remove the imaginary part from numbers like x + 0.0i in R

I assume that there will already be some answers to this, so I apologize in advance, but I tried to look without luck, so .....

.... What is the smart way to deal with form numbers x + 0.0i in R?

For example, suppose that

y <- 1 + 0.0i 

So of course:

 y == 1 

truly. So what is the best way to make y be 1.

Edit:

I do not want to discard the imaginary part, unless round(Im(y)) == 0 , but I was hoping that you would not have to explicitly check this.

+4
source share
1 answer

I can't think of anything better than this (yes, it is surprising that there is no built-in imaginary-creaking function ... or maybe someone else will come up with one)

 f <- function(x) { if (all(Im(z <- zapsmall(x))==0)) as.numeric(z) else x } 
+3
source

Source: https://habr.com/ru/post/1447824/


All Articles