28

I have a data table ("norm") containing numeric - at least to what I can see - normalized values of the following form:

A screenshot of the table

When I am executing

k <- kmeans(norm,center=3)

I am receving the following error:

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

Can you help me? Thank you!

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
Jonathan Rhein
  • 1,616
  • 3
  • 23
  • 47
  • 1
    Have you checked if there's `NaN/NA/Inf` in your data? You can check using `is.na()` and `is.finite()` functions – Ujjwal Kumar Apr 07 '16 at 07:46
  • Yes, there is plenty of `NAs` in my file sheet, but I thought that shouldnt be a problem?! `is.finite()` returns a lot of `TRUEs` but also some `FALSEs`. How can i fix this? – Jonathan Rhein Apr 07 '16 at 07:52
  • 1
    You would have to remove the `NA/Inf/NaN` values from your data. See "missing value imputation" methods for details. One simple method is replacing them by row/column mean values. – Ujjwal Kumar Apr 07 '16 at 10:03
  • @UjjwalKumar Thank you! – Jonathan Rhein Apr 08 '16 at 05:33

5 Answers5

30

kmeans cannot handle data that has NA values.

The mean and variance are then no longer well defined, and you don't know anymore which center is closest.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
17

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

This error occurs also due to non numeric values present in the table.

Richie
  • 171
  • 1
  • 5
4

all of you all who are having " Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)" problem instead of

results <- kmeans(iris.features,3)
results

write the following and please be careful of the case in iris write whatever you have used in the beginning

results <- kmeans(na.omit(irisa.features),3) # this helps in omitting NA 
results
3

For error stating:

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

the dataset you have posted above contains scaled entries, the reason must be that you have NA values in your dataset, hence omit them by the following code.

km_cluster <- kmeans(na.omit(MyData), 3)
km_cluster
km_cluster$withinss
km_cluster$tot.withinss/km_cluster$betweenss
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
SMR
  • 401
  • 4
  • 15
1

You'll also get this error if you pass a grouped dataframe to the kmeans function. I prepared my data using dplyr and had to close with ungroup() to use kmeans after.

jkbfbn
  • 11
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 31 '22 at 01:45