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.

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 vector
x <- 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.

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] 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:
# [1] 12.663139  4.399086  4.661347
#  (between_SS / total_SS =  78.1 %)
# 
# Available components:
# 
# [1] "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:

 

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.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.

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.


6 Comments. Leave new

  • yes sir tried it but still getting the same error in it even though I tried to remove the values from the columns of the dataset
    Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
    In addition: Warning message:
    In storage.mode(x) <- "double" : NAs introduced by coercion

    Reply
  • How can I fix this error ?
    HiperOptikcluster <- kmeans(HiperOptik,9,nstart = 100)
    Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
    In addition: Warning message:
    In storage.mode(x)
    this is my data
    1 CR000358 2,3 1 20.07.2019 SC34056 ISTANBUL Aksesuar
    2 CR000536 35,5 1 10.03.2019 SC34073 ISTANBUL Güneş Gözlüğü
    3 CR000592 10,2 1 13.08.2019 SC34062 ISTANBUL Güneş Gözlüğü
    4 CR000704 58 1 19.03.2019 SC34002 ISTANBUL Optik Çerçeve
    5 CR000991 68,6 1 3.05.2019 SC34024 ISTANBUL Güneş Gözlüğü
    6 CR001029 38,1 1 10.05.2019 SC34045 ISTANBUL Güneş Gözlüğü
    7 CR001101 0,7 1 7.06.2019 SC34026 ISTANBUL Aksesuar
    8 CR001101 1,3 2 19.08.2019 SC34026 ISTANBUL Aksesuar
    9 CR001109 2,3 1 29.07.2019 SC34016 ISTANBUL Aksesuar
    10 CR001404 34,6 1 5.05.2019 SC34069 ISTANBUL Güneş Gözlüğü
    11 CR001404 40,5 1 6.05.2019 SC34026 ISTANBUL Güneş Gözlüğü
    12 CR001511 19,8 1 9.08.2019 SC34023 ISTANBUL Lens
    13 CR001530 11,9 2 21.07.2019 SC34009 ISTANBUL Cam
    14 CR001571 23,7 1 27.03.2019 SC34016 ISTANBUL Optik Çerçeve
    15 CR001614 10,7 2 3.05.2019 SC34024 ISTANBUL Cam
    16 CR001872 33,1 2 1.08.2019 SC34018 ISTANBUL Cam
    17 CR001998 0,7 1 4.08.2019 SC34005 ISTANBUL Aksesuar
    18 CR002183 2,3 1 9.08.2019 SC34017 ISTANBUL Aksesuar
    19 CR002183 2,5 1 27.08.2019 SC34017 ISTANBUL Bakım Solüsyon
    20 CR002183 6,3 1 13.03.2019 SC34017 ISTANBUL Optik Çerçeve
    21 CR002183 6,3 1 12.04.2019 SC34017 ISTANBUL Optik Çerçeve
    22 CR002183 6,3 1 25.07.2019 SC34017 ISTANBUL Optik Çerçeve
    23 CR002183 10,8 1 19.08.2019 SC34017 ISTANBUL Güneş Gözlüğü
    24 CR002183 13,2 1 15.06.2019 SC34017 ISTANBUL Güneş Gözlüğü
    25 CR002183 13,9 1 21.06.2019 SC34017 ISTANBUL Güneş Gözlüğü
    26 CR002183 22,9 1 29.06.2019 SC34017 ISTANBUL Güneş Gözlüğü
    27 CR002183 62,4 1 4.03.2019 SC34017 ISTANBUL Güneş Gözlüğü
    28 CR002183 70,8 1 25.04.2019 SC34017 ISTANBUL Güneş Gözlüğü
    29 CR002223 1,7 1 27.05.2019 SC34017 ISTANBUL Aksesuar
    30 CR002223 2 1 27.05.2019 SC34017 ISTANBUL Aksesuar
    31 CR002223 2,9 1 28.04.2019 SC34017 ISTANBUL Bakım Solüsyon
    32 CR002223 6,3 1 3.03.2019 SC34017 ISTANBUL Optik Çerçeve
    33 CR002223 6,3 1 23.03.2019 SC34017 ISTANBUL Optik Çerçeve
    34 CR002223 6,3 1 8.08.2019 SC34017 ISTANBUL Optik Çerçeve
    35 CR002223 7,5 1 18.08.2019 SC34017 ISTANBUL Bakım Solüsyon
    36 CR002223 11,5 1 18.08.2019 SC34017 ISTANBUL Güneş Gözlüğü
    37 CR002223 15,7 1 28.04.2019 SC34017 ISTANBUL Optik Çerçeve
    38 CR002484 15,6 1 16.08.2019 SC34017 ISTANBUL Güneş Gözlüğü
    39 CR003012 317,5 2 13.06.2019 SC34024 ISTANBUL Cam
    40 CR003416 0,7 1 14.04.2019 SC34072 ISTANBUL Aksesuar
    41 CR000005 59,5 8 20.05.2019 SC34039 ISTANBUL Lens
    42 CR000231 24,9 2 17.05.2019 SC34047 ISTANBUL Cam
    43 CR000287 14,6 1 11.08.2019 SC34011 ISTANBUL Lens
    44 CR000441 0,7 1 3.08.2019 SC34026 ISTANBUL Aksesuar
    45 CR000870 38,1 1 5.06.2019 SC34037 ISTANBUL Güneş Gözlüğü
    46 CR000877 38,2 1 7.05.2019 SC34006 ISTANBUL Optik Çerçeve
    47 CR001083 42,4 1 23.06.2019 SC34012 ISTANBUL Güneş Gözlüğü
    48 CR001101 0,7 1 12.07.2019 SC34026 ISTANBUL Aksesuar
    49 CR001101 2,3 1 23.04.2019 SC34026 ISTANBUL Aksesuar
    50 CR001101 15,6 1 24.06.2019 SC34026 ISTANBUL Güneş Gözlüğü
    51 CR001161 65,4 1 1.08.2019 SC34029 ISTANBUL Güneş Gözlüğü
    52 CR001384 2,3 1 29.08.2019 SC34048 ISTANBUL Aksesuar
    53 CR001397 0 2 5.07.2019 SC34031 ISTANBUL Cam
    54 CR001397 35,9 2 5.07.2019 SC34031 ISTANBUL Cam
    55 CR001456 0,7 1 6.04.2019 SC34010 ISTANBUL Aksesuar
    56 CR001544 5,9 1 5.06.2019 SC34043 ISTANBUL Güneş Gözlüğü
    57 CR001555 17,2 1 8.07.2019 SC34070 ISTANBUL Lens
    58 CR001624 2,5 1 29.05.2019 SC34017 ISTANBUL Bakım Solüsyon
    59 CR001974 29,8 4 18.05.2019 SC34020 ISTANBUL Lens
    60 CR002027 39,6 1 10.03.2019 SC34028 ISTANBUL Optik Çerçeve
    61 CR002183 0,7 1 31.03.2019 SC34017 ISTANBUL Aksesuar
    62 CR002183 0,7 1 31.03.2019 SC34017 ISTANBUL Aksesuar
    63 CR002183 0,7 1 31.07.2019 SC34017 ISTANBUL Aksesuar
    64 CR002183 6,2 1 22.07.2019 SC34017 ISTANBUL Optik Çerçeve
    65 CR002183 10,8 1 15.04.2019 SC34017 ISTANBUL Güneş Gözlüğü
    66 CR002183 11,5 1 17.07.2019 SC34017 ISTANBUL Güneş Gözlüğü
    67 CR002183 14,4 1 25.03.2019 SC34017 ISTANBUL Güneş Gözlüğü
    68 CR002183 21 1 3.05.2019 SC34017 ISTANBUL Optik Çerçeve
    69 CR002183 24,6 1 21.06.2019 SC34017 ISTANBUL Güneş Gözlüğü
    70 CR002183 25 1 7.07.2019 SC34017 ISTANBUL Optik Çerçeve
    71 CR002183 27,7 1 3.06.2019 SC34017 ISTANBUL Güneş Gözlüğü
    72 CR002183 39,6 1 2.07.2019 SC34017 ISTANBUL Optik Çerçeve
    73 CR002183 72,6 1 28.08.2019 SC34017 ISTANBUL Optik Çerçeve
    74 CR002183 81,7 1 28.07.2019 SC34017 ISTANBUL Güneş Gözlüğü
    75 CR002183 85,8 1 19.08.2019 SC34017 ISTANBUL Optik Çerçeve
    76 CR002223 0,7 1 28.04.2019 SC34017 ISTANBUL Aksesuar
    77 CR002223 2,5 1 1.08.2019 SC34017 ISTANBUL Bakım Solüsyon
    78 CR002223 19,7 1 27.07.2019 SC34017 ISTANBUL Optik Çerçeve
    79 CR002223 32,9 1 7.03.2019 SC34017 ISTANBUL Optik Çerçeve
    80 CR002223 53,9 1 4.05.2019 SC34017 ISTANBUL Güneş Gözlüğü
    81 CR002223 79,2 1 28.08.2019 SC34017 ISTANBUL Güneş Gözlüğü
    82 CR002285 14,4 1 17.03.2019 SC34019 ISTANBUL Güneş Gözlüğü
    83 CR002301 0,7 1 31.08.2019 SC34024 ISTANBUL Aksesuar
    84 CR002385 52,9 2 9.08.2019 SC34035 ISTANBUL Cam
    85 CR002450 42,4 1 7.08.2019 SC34046 ISTANBUL Güneş Gözlüğü
    86 CR002497 0,7 1 18.07.2019 SC34042 ISTANBUL Aksesuar
    87 CR003180 2 1 22.04.2019 SC34012 ISTANBUL Bakım Solüsyon
    88 CR003180 32,7 3 22.04.2019 SC34012 ISTANBUL Lens
    89 CR003416 1,9 1 31.03.2019 SC34072 ISTANBUL Bakım Solüsyon
    90 CR003416 7,3 1 6.05.2019 SC34072 ISTANBUL Güneş Gözlüğü
    91 CR003416 34,9 1 13.08.2019 SC34072 ISTANBUL Güneş Gözlüğü
    92 CR003416 36,3 1 7.04.2019 SC34072 ISTANBUL Güneş Gözlüğü
    93 CR003520 65,4 1 28.06.2019 SC34020 ISTANBUL Güneş Gözlüğü
    94 CR003570 14,4 1 10.07.2019 SC34031 ISTANBUL Güneş Gözlüğü
    95 CR003632 8,8 1 12.08.2019 SC34071 ISTANBUL Güneş Gözlüğü
    96 CR004235 82,3 1 2.08.2019 SC34084 ISTANBUL Optik Çerçeve
    97 CR004235 113,9 2 2.08.2019 SC34084 ISTANBUL Cam
    98 CR004427 10,1 1 22.07.2019 SC34028 ISTANBUL Güneş Gözlüğü
    99 CR004671 27,7 1 1.03.2019 SC34052 ISTANBUL Güneş Gözlüğü
    100 CR004784 1,2 2 7.03.2019 SC34037 ISTANBUL Cam
    101 CR004842 4,6 2 22.04.2019 SC34062 ISTANBUL Cam
    102 CR004952 28 2 14.04.2019 SC34002 ISTANBUL Cam
    103 CR005100 4,1 2 20.05.2019 SC34011 ISTANBUL Cam
    104 CR005103 20,7 1 23.06.2019 SC34045 ISTANBUL Güneş Gözlüğü
    105 CR005326 50,6 1 21.06.2019 SC34011 ISTANBUL Güneş Gözlüğü
    106 CR005326 64,6 1 3.04.2019 SC34011 ISTANBUL Güneş Gözlüğü
    107 CR005569 14,9 1 4.05.2019 SC34027 ISTANBUL Güneş Gözlüğü
    108 CR005617 5,6 1 28.05.2019 SC34007 ISTANBUL Cam
    109 CR005698 0 2 10.08.2019 SC34009 ISTANBUL Cam
    110 CR005875 88,6 1 23.06.2019 SC34056 ISTANBUL Güneş Gözlüğü
    111 CR006033 39,7 4 19.07.2019 SC34006 ISTANBUL Lens
    ProductCode ProductProductivityName

    Reply
    • Hi Eda,

      The kmeans function takes a “numeric matrix of data, or an object that can be coerced to such a matrix” as input. However, your data contains many non-numeric columns.

      To which part of your data should the kmeans function be applied?

      Regards,
      Joachim

      Reply
  • Hi Joachim,

    I am using the package Pursuit to construct projection pursuit index, using the function “res <- PP_Optimizer(data=data, findex="friedmantukey")", but then having the error "Error in PP_Index(data = Proj, class = class, vector.proj = Aa, findex = findex, : NA/NaN/Inf in foreign function call (arg 3)".

    One of the variables in my data is binary categorical data, with all values 1. Is the the reason causing the above error? How should I fix it?

    Many Thanks!!

    Reply
    • Hello Jennifer,

      The fact that one of your variables is a binary categorical variable with only a single value could be a potential cause of this issue. In projection pursuit, we are interested in finding informative projections of the data. If a variable contains no variation (i.e., all values are the same), it will not contribute any useful information to the projection and might be causing numerical instability or other issues in the calculations.

      Here are a few steps to address and potentially resolve the error:

      Remove Constant Variables: Before running the projection pursuit function, remove any variables that are constant (i.e., have the same value for all observations). You can do this in R using the apply function. For example, to keep only variables with some variation:

      data <- data[, apply(data, 2, var) > 0]

      If the problem persists then:

      Check for NA/NaN/Inf Values: Ensure that there are no NA, NaN, or Inf values in your data. You can use the is.na, is.nan, and is.infinite functions to identify such values. If you find any, decide on an appropriate method to handle them (e.g., imputation or removal).

      Standardize the Data: Some algorithms behave better when the input data is standardized (mean of 0 and standard deviation of 1 for each variable). You can use the scale function in R for this purpose:

      data <- scale(data)

      I hope one of these solutions helps.

      Best,
      Cansu

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top