# R Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

This article illustrates how to handle the “Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)” in the R programming language.

Let’s dive right in:

## Constructing Example Data

The following data is used as basement for this R programming tutorial.

```set.seed(56389) # Create numeric example vector x <- c(NA, rnorm(100)) head(x) # Head of example vector #  NA -1.72955699 1.57449671 -1.52165311 0.48225317 0.09359781```

The previous output of the RStudio console shows the structure of our example data – It is a random numeric vector that contains one NA value at the first index position.

## Example 1: Reproduce the Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

The following R programming syntax shows how to replicate the error message “NA/NaN/Inf in foreign function call (arg 1)” when using the kmeans function in R.

Let’s assume that we want to apply the kmeans function to group our example vector into three clusters. Then, we may try to use the following R code:

```kmeans(x, 3) # Trying to apply kmeans function # Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)```

Unfortunately, the RStudio console returns the “Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)”.

The reason for this is that our input vector contains an NA value and the kmeans function cannot handle missing data.

Note that we have reproduced this error message based on a vector containing NA values. However, the same error message occurs when our input data contains non-numeric values that are not properly formatted as numbers.

So how can we solve this problem in R?

## Example 2: Fix the Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)

This example illustrates how to debug the “Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)”.

For this, we have to remove all NA values from our vector using the is.na function:

```kmeans(x[!is.na(x)], 3) # Remove NA values # K-means clustering with 3 clusters of sizes 26, 45, 29 # # Cluster means: # [,1] # 1 -1.21408608 # 2 0.06647359 # 3 1.16156234 # # Clustering vector: #  1 3 1 2 2 1 3 3 2 2 2 1 2 2 3 3 2 2 3 2 2 2 1 1 2 1 2 2 3 2 3 1 2 2 1 3 2 1 2 1 3 1 1 2 3 2 2 2 1 3 1 2 3 2 3 1 1 3 1 2 1 1 3 2 2 1 2 3 2 2 1 1 2 3 2 2 2 2 3 3 1 3 2 3 3 2 3 2 3 3 3 1 3 2 2 3 1 2 2 2 # # Within cluster sum of squares by cluster: #  12.663139 4.399086 4.661347 # (between_SS / total_SS = 78.1 %) # # Available components: # #  "cluster" "centers" "totss" "withinss" "tot.withinss" "betweenss" "size" "iter" "ifault"```

As you can see, the previous R code produced a valid output without any error or warning messages.

## Video & Further Resources

Do you want to learn more about errors in R? Then you may want to watch the following video of my YouTube channel. In the video, I’m explaining the examples of this tutorial:

Besides the video, you might want to have a look at the other tutorials that I have published on www.statisticsglobe.com:

To summarize: You have learned in this tutorial how to deal with the “Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)” in R.