**R – R-BAR**, and kindly contributed to R-bloggers)

R’s **Inf** keyword – Have you’ve ever wondered what to do with it? If so, this is the second in series of posts that explore how we can exploit the keyword’s interesting properties to get the answers we need and improve code robustness. If you want to catch up on the first post where we look at Inf and the cut() function, please see Infamous Inf – Part I

For those unfamiliar with R’s Inf keyword, it is defined as a positive or negative number divided by zero yielding positive or negative infinity, respectively.

c(plus_inf = 1/0, minus_inf = -1/0) # plus_inf minus_inf # Inf -Inf

Sounds very theoretical. So how we can make practical use of infinity in R? In this first post, we’ll be discussing how Inf can make binning data with cut() a more robust process.

## Inf with integrate()

For many of us, Integration ranks low in the fun category, but sometimes its necessary. For example, to get the probability of a normally distributed event between two limits you would need to integrate the normal distribution density function between limits a and b. The functions are detailed below along with their R function equivalent.

The total area under the Density Function should be 1. So if we integrate dnorm(x) from negative to positive infinity we should get 1. Let’s see

LongDF <- function(x, mu = 0 , sigma = 1){ 1/sqrt(2*pi*sigma^2) * exp(-(x-mu)^2/(2*sigma^2)) } #dnorm and the long function are the same... #dnorm(0) == LongDF(0) #TRUE integrate(dnorm, -Inf, Inf)

The result:

1 with absolute error < 9.4e-05

As expected the answer is 1. The same result you’d get if you ran.

pnorm(q = Inf, mean = 0, sd = 1)

Which also yields, 1

**leave a comment**for the author, please follow the link and comment on their blog:

**R – R-BAR**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...