Đồ chơi trẻ em bé na chơi play

      162

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.

Bạn đang xem: Đồ chơi trẻ em bé na chơi play

Table of contents:


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 vectorx c(NA, rnorm(100))head(x) # Head of example vector# <1> 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.

Xem thêm: Uống Trà Lipton Có Tác Dụng Gì ? Uống Nhiều Có Tốt Không Lợi Ích Của Việc Uống Trà Đen

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:


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:

The YouTube video will be added soon.

Besides the video, you might want to have a look at the other tutorials that I have published on www.xedapdientot.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.

Please let me know in the comments section, in case you have additional questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.I hate spam & you may opt out anytime: Privacy Policy.