# Infinity in R: How to Handle Infinity in R

Infinity is the concept of something unlimited, endless, without boundary in mathematics. In math, infinity is a concept that refers to an endless quantity that’s larger than every real number. Infinity can appear if you divide any number by 0 in mathematics.

## Types of Infinity in R

There are two types of infinity in R.

1. Positive Infinity(Inf)
2. Negative Infinity(-Inf)

The Inf and -Inf are positive and negative infinity, whereas NaN means ‘Not a Number’.

Let’s see how we can create an infinity in R.

``````data <- 21

div <- data / 0
div``````

#### Output

``[1] Inf``

You can see that R correctly tells you the result is Inf or infinity. The Negative infinity is shown as -Inf.

## How to Handle Infinity in R

To handle the infinity in R, use one of the following functions.

1. is.finite(data)
2. is.infinite(data)

### is.finite() in R

The is.finite() function returns the vector of the same length as input x, the jth element of which is TRUE  if x[j] is finite.

``````data <- 21

div <- data / 0
is.finite(div)``````

#### Output

``[1] FALSE``

It returns FALSE because the div is infinity and is.infinity() function returns FALSE if the element is infinity. So this is one way to check the value is infinity or not.

### is.infinite() in R

The is.infinite() is a built-in R function that returns TRUE if the element is infinity; otherwise, it will not.

``````data <- 21

div <- data / 0
is.infinite(div)``````

#### Output

``[1] TRUE``

As you can see that the div has an infinity value that is why it returns TRUE.

## Dealing with undefined outcomes in R

What if you divide infinity by infinity. Let’s explore that scenario.

``````div <- Inf / Inf
div``````

#### Output

``[1] NaN``

As you can see that if you divide the Inf by Inf, then it will return NaN.

If you divide 0 by o, then also you will get NaN.

``````div <- 0 / 0
div``````

#### Output

``[1] NaN``

To check the NaN value in R, use the is.nan() function.

That is it for infinity in the R tutorial.

Categories R