Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1) [closed]
Asked Answered
B

6

27

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!

Businesswoman answered 7/4, 2016 at 7:40 Comment(4)
Have you checked if there's NaN/NA/Inf in your data? You can check using is.na() and is.finite() functionsNamhoi
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?Businesswoman
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.Namhoi
@UjjwalKumar Thank you!Businesswoman
L
31

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.

Lionhearted answered 9/4, 2016 at 19:10 Comment(1)
Kmeans cannot handle data that has NA, INF and CONTANTS.Whittemore
H
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.

Heterotopia answered 31/7, 2018 at 9:48 Comment(0)
B
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
Blubberhead answered 12/11, 2017 at 6:33 Comment(0)
P
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
Presentiment answered 19/7, 2018 at 0:11 Comment(0)
F
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.

Fronia answered 30/8, 2022 at 14:38 Comment(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.Melee
H
0

This error can also be caused by scaling data with a column of values that do not vary (ie, are constant). This is because the standard deviation of those columns equals zero, and scaling produces NaN values. The code below fixes this programmatically by excluding columns with no variation.

# Data
df=copy(mtcars)
df$Constant=2

# Kmeans
fit=kmeans(scale(df),centers=2) # Error

# Identify columns with SD=0
tmp=sapply(df,sd)
tmp=names(tmp)[tmp==0]

# Kmeans
tmp1=setdiff(names(df),tmp)
fit=kmeans(scale(df[,tmp1]),centers=2) # Works
Helmer answered 15/5 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.