R Basic

What is is.finite() Function in R

In the process of data cleaning, you often need to identify and remove non-finite values from the datasets before analysis. The actual non-finite (infinite) values are Inf(positive infinity) and –Inf(negative infinity). NA and NaN are special cases.

is.finite()

R is.finite() function checks whether input objects are finite. By default, it handles atomic vectors, but it can be numeric vectors, matrices, and arrays. It returns a logical vector of the same length where TRUE suggests that the corresponding element is a finite number, and FALSE suggests that it is not.

The is.finite() method does not work directly with Data Frames because it is not a list of vectors of potentially different types. If you try to work with data frames, it will give you an error like this: “Error in is.finite(df) : default method not implemented for type ‘list'”.

Syntax

is.finite(obj)

Parameters

Name Value
obj It is an input R object that will be checked for finite values.

Return value

  1. If an element is a regular number, is.finite() returns TRUE.
  2. If an element is Inf or -Inf, it returns FALSE.
  3. If an element is NA (Not Available) or NaN (Not A Number), it returns FALSE.

Finite and Infinite Numbers

vec <- c(1, -1, Inf, 0, -Inf)

is.finite(vec)

# Output
# [1] TRUE TRUE FALSE TRUE FALSE

You can see that the output vector’s length is the same as the input vector’s length, and infinite numbers are replaced by FALSE. NaN represents an undefined numeric number.

Passing “NA”

vec <- c(1, NA, -1, NA)

is.finite(vec)

# Output
# [1] TRUE FALSE TRUE FALSE

Extracting finite values from the vector

Using vector indexing, we can extract the finite values from the vector with the help of is.finite() function.

vec <- c(1, NA, -1, NA)

finite_vals <- vec[is.finite(vec)]

print(finite_vals)

# Output: [1] 1 -1

Matrices

We can also check for finite values in the matrix. It will return TRUE for a finite number in individual elements and FALSE otherwise.

mat <- matrix(c(1, NaN, 3, Inf, 5, 6), nrow = 2, ncol = 3)
print("Finite values in matrix:")
print(is.finite(mat))

# Output

# [1] "Finite values in matrix:"

#       [,1]  [,2]  [,3]
# [1,] TRUE  TRUE   TRUE
# [2,] FALSE FALSE  TRUE

You can see that the output matrix has the same size as the input matrix. Only its elements are boolean values.

Arrays

Let’s create a 3D array with a mix of finite, infinite, and NaN numbers.

# Defining an array
arr <- array(c(1, 2, 3, Inf, NaN, -Inf, 7, 8), dim = c(2, 2, 2))

print("Original Array:")
print(arr)

# Applying is.finite() function to the array
finite_arr <- is.finite(arr)
print(finite_arr)

Output

You can see that the output is a logical array of the same dimension as an input array. 

That’s all, mates!

Recent Posts

summary() Function: Producing Summary Statistics in R

The summary() is a generic function that produces the summary statistics for various R objects,…

5 hours ago

R paste() Function

The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…

1 week ago

paste0() Function in R

R paste0() function concatenates strings without any separator between them. It is a shorthand version…

1 week ago

How to Calculate Standard Error in R

Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…

2 weeks ago

R max() and min() Functions

max() The max() function in R finds the maximum value of a vector or data…

2 weeks ago

R as.Date() Function: Working with Dates

The as.Date() function in R converts various types of date and time objects or character…

2 weeks ago