R Basic

What is is.infinite() Function in R

The is.infinite() function checks each element of a vector or an R object contains infinity. If an element is infinite, the corresponding element in the output vector is TRUE. Otherwise, FALSE. 

By default, it handles atomic vectors, but it can also handle other data types.

In R, there are two types of infinity:

  1. Inf (positive infinity)
  2. -Inf (negative infinity)

The output object of is.infinite() function has the same length as an input object with the difference that it will be filled with logical values (TRUE or FALSE).

In mathematical calculations, infinite values can disrupt our calculations if they are not appropriately handled. Instances like division by 0, numerical overflow, or log(0) can produce infinite results.

Syntax

is.infinite(obj)

Parameters

Name Value
obj It is an input R object. It can be anything like a vector, list, factor, data frame, etc.

Numeric vector

# Basic usage
vec <- c(11, Inf, -Inf, 21, 19)
is.infinite(vec) # Output: FALSE TRUE TRUE FALSE FALSE

# Division by zero
y <- 21 / 0
is.infinite(y) # Output: TRUE

z <- -19 / 0
is.infinite(z) # Output: TRUE

# Large numbers
a <- 1e308 # It is a large number, but still finite
is.infinite(a) # Output: FALSE

Here, you can see in the above program that we covered most of the basic scenarios where infinity can be generated in the output, and we handled it.

We can use the if statement further to handle it properly.

# Division by zero
y <- 21 / 0
is.infinite(y) # Output: TRUE

# Adding if statement to avoid division by zero
if (y == Inf) {
  y <- 0
}

print(y) # Output: 0

Complex vector

If your input object is a complex vector, the is.infinite() function checks whether the real or imaginary part is infinite.

# Define complex vector with inf and -inf
complex_vec <- c(1 + 2i, Inf + 0i, 3 - Inf * i, 0 + 0i)

# Checking for infinite values
is.infinite(complex_vec) # Output: FALSE TRUE TRUE FALSE

For 1+2i, both real and imaginary parts are finite, so it returns FALSE.

For Inf+oi, the real part is infinite, and the imaginary part is finite, so it returns TRUE since one of them is infinite.

For 3-inf*i, the real part is finite, but the imaginary part is infinite, so it returns TRUE.

For 0+0i, both real and imaginary parts are finite, so it returns FALSE.

Handling NA and NaN values

The is.infinite() function is specifically designed to handle Inf or -Inf values. It does not handle NA and NaN values properly.

exceptional_vec <- c(Inf, -Inf, NA, NA, 0 / 0)

is.infinite(exceptional_vec) # Output: TRUE TRUE FALSE FALSE FALSE

You can see in the above program that it returns FALSE for NA, NaN, and 0 / 0.

To detect NA values, use is.na() function.

To detect NaN values, use is.nan() function.

Factor

The Factor is a tricky data type because factors are stored as integers with associated levels and integers will never be Inf or -Inf. So, you cannot apply is.infinite() function to a factor directly because it always returns FALSE.

my_fact <- factor(c("a", "b", "c", "d", "e"))

is.infinite(my_fact) # FALSE FALSE FALSE FALSE FALSE

To check for infinite values, you must convert the factor to a numeric type if the levels themselves represent numeric values. For that, you need conversion functions like as.character() and as.numeric().

numeric_factor <- factor(c("21", "Inf", "-19", "-Inf"))

is.infinite(as.numeric(as.character(numeric_factor))) # Output: FALSE TRUE FALSE TRUE

List

The list is a collection of different types of data, and here also, we cannot directly apply this function. What we can do here is apply is.infinite() function to individual elements of the list using lapply() or sapply() functions.

# Define a list with Inf and -Inf values
main_list <- list(c(11, Inf, 21), c(-Inf, 19, 21), "KB")

is_infinite_list <- lapply(main_list, is.infinite)

print(is_infinite_list)

Output

Data Frame

Data Frame is also a complex type in which what you can do is to apply is.infinite() function to individual columns or use lapply() or apply() to apply it to all numeric columns.

# Define a data frame where Inf and -Inf values are present
df <- data.frame(
  col1 = c(21, Inf, 3),
  col2 = c(-Inf, 15, 19),
  col3 = c("KB", "KL", "KE")
)

# Applying to a specific column
is.infinite(df$col1) # Output: FALSE TRUE FALSE

# Applying to only numeric columns
numeric_cols <- sapply(df, is.numeric)

lapply(df[, numeric_cols], is.infinite)

# Output:
# $col1
# [1] FALSE TRUE FALSE

# $col2
# [1] TRUE FALSE FALSE

That’s all!

Recent Posts

R length(): Vector, List, Matrix, Array, Data Frame, String

Before executing an operation on an object, it is advisable to check its length, as…

15 hours ago

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…

2 days ago

Adding Single or Multiple Columns to Data Frame in R

Whether you want to add new data to your existing datasets or create new variables…

4 days ago

sqrt() Function: Calculate Square Root in R

The square root of a number is a value that is multiplied by itself, giving…

5 days ago

How to Remove Duplicate Rows from DataFrame in R

Duplicate rows refer to all the values across all columns that are the same in…

6 days ago

How to Remove NA From Vector in R

A vector is a data structure that holds the same type of data. When working…

1 week ago