In R, how do I create a function where all the numbers in a vector are rescaled, Inf becomes 1, and -Inf becomes 0?

Viewed 37

I'm a beginner in R and have an assignment where I have to create a function where all the numbers are rescaled, Inf is mapped to 1, and -Inf is mapped to 0. I understand how to do the rescale, I just don't know how to add to the function so Inf becomes 1 and -Inf becomes 0. If anyone could at least point me in the right direction or let me know what kind of commands in R will allow me to do that, it would be greatly appreciated!

Here is my code so far:

rescale01<-function(x){  
   rng<-range(x, na.rm=TRUE, finite=TRUE)
    (x-rng[1])/(rng[2]-rng[1])
  }

Thanks in advance for your help.

1 Answers

You can try atan

f <- function(x) 0.5+atan(x)/pi
Related