Solving the 'Error in Hclustfun(distr): na/nan/inf in Foreign Function Call (Arg 11)' – Step-By-Step Guide and Troubleshooting Tips

This guide will walk you through how to solve the 'Error in Hclustfun(distr)': na/nan/inf in Foreign Function Call (Arg 11) that you may encounter while working with hierarchical clustering in R. We'll provide you with step-by-step instructions and troubleshooting tips to help you resolve this issue.

Table of Contents

  1. Introduction to the Error
  2. Step-By-Step Guide to Solving the Error
  3. Troubleshooting Tips
  4. FAQ
  5. Related Links

Introduction to the Error

The 'Error in Hclustfun(distr): na/nan/inf in Foreign Function Call (Arg 11)' occurs when using the hclust function in R to perform hierarchical clustering on a dataset. This error is typically caused by the presence of missing values (NA), infinite values (Inf), or NaN (not a number) values in the input data. When these values are present in the data, the hclust function is unable to compute the distance matrix, resulting in the error.

Step-By-Step Guide to Solving the Error

To resolve this error, follow the steps below:

Step 1: Check for NA, NaN, and Inf values in your data

Before performing hierarchical clustering, it's essential to ensure that your data does not contain any missing, infinite, or NaN values. You can use the following functions to check for these values in your dataset:

# Check for NA values

# Check for NaN values

# Check for Inf values

If any of these functions return TRUE, it means that your data contains the corresponding problematic values.

Step 2: Handle missing, infinite, and NaN values in your data

If your data contains any problematic values, you'll need to handle them before performing hierarchical clustering. There are several ways to handle these values:

  1. Remove rows with missing, infinite, or NaN values: You can use the na.omit() or complete.cases() functions to remove rows containing any problematic values.
your_data_clean <- your_data[complete.cases(your_data), ]
  1. Impute missing or NaN values: You can use the na.approx() function from the zoo package or the imputeTS package to impute missing or NaN values in your dataset.
your_data_clean <- na.approx(your_data)
  1. Replace infinite values: You can use the following code to replace infinite values with a suitable large value or the maximum finite value in your dataset.
your_data_clean <- your_data
your_data_clean[is.infinite(your_data)] <- max(your_data[is.finite(your_data)], na.rm=TRUE)

Step 3: Perform hierarchical clustering

After cleaning your data, you can now perform hierarchical clustering using the hclust function without encountering the error.

# Compute the distance matrix
distr <- dist(your_data_clean)

# Perform hierarchical clustering
clustering <- hclust(distr)

Troubleshooting Tips

Ensure that you've imported the necessary packages: Make sure that you have imported the required packages (e.g., zoo, imputeTS) before using their functions.

Double-check your data cleaning: After handling problematic values in your data, re-run the code in Step 1 to ensure that your cleaned dataset does not contain any missing, infinite, or NaN values.

Check the data type of your input data: Ensure that your input data is of the correct data type (e.g., numeric) before performing hierarchical clustering.


1. What is hierarchical clustering?

Hierarchical clustering is a type of unsupervised machine learning algorithm used to group similar objects into clusters. It builds a hierarchy of clusters by either a bottom-up approach (agglomerative clustering) or a top-down approach (divisive clustering).

2. What is the hclust function in R?

The hclust function in R is used to perform hierarchical clustering on a dataset. It takes a distance matrix as input and returns an object of class hclust that describes the hierarchical clustering.

3. What is a distance matrix?

A distance matrix is a square matrix that contains the distances between all pairs of objects in a dataset. In the context of hierarchical clustering, it is used to determine which objects are similar and should be grouped together.

4. What are the common distance measures used in hierarchical clustering?

Some common distance measures used in hierarchical clustering include Euclidean distance, Manhattan distance, and cosine similarity.

5. How can I visualize the results of hierarchical clustering?

You can use the plot function in R to create a dendrogram, which is a tree-like visualization of the hierarchical clustering.

plot(clustering, hang = -1)

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.