• Register
1 vote

Problem :

Currently I am learning r. I have the data table "normal" which has the numeric as far as I know normalized values.

If I try to execute the below command

k <- kmeans(normal,center=3)

Then I always face the below error:

“Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)”
I am trying to fix it by researching on it but still unable to fix it.
Can somebody help me in fixing it?
8 5 2
3,230 points

1 Answer

1 vote

Solution :

I guess your table has lots of NaN/NA/Inf .You can confirm it using the is.na() and is.finite() functions.
You need to remove all the NA/Inf/NaN values from the data. You can take help of "missing value imputation" functions for more details. This one simple method is helpful in replacing NA/Inf/NaN by the row/column mean values. 
The kmeans is unable to handle the data which has NA values.In that case the mean and variance are no longer well defined and also you cant confirm anymore about which center is closest. You can also try to omit the NA values which are present in your table with the use of below code.
km_cluster <- kmeans(na.omit(MyData), 3)

Hope my above solution helped you in fixing your issues.

8 4
5,680 points

Related questions

0 votes
1 answer 122 views
Problem : I have done a lot of research on this extensively without finding any solution on it. I have tried cleaning my data set as follows: library("myraster") impute.mean <- function(l) replace(l, is.na(l) | is.nan(l) | is.infinite(l) , mean(l, na.rm = TRUE)) losses <- ... TRUE, na.rm=TRUE, nan.rm=TRUE) All my research says it should be NA's or Inf's or NaN's in the data but I don't have any
asked Dec 5, 2019 alecxe 7.5k points
1 vote
1 answer 1.4K views
Problem : I want to perform a logistic regression but I am facing following error I am unable to find my mistake. summary(glm(prefmerkel~angst+crisismerkel+leadership+trustworthiness+ideology+pid+agegroups+gender+region,data=gles)) Error in glm.fit(x = c(1, 1, 1, 1, 1, 1, 1, ... In Ops.factor(eta, offset) : -' nicht sinnvoll für Faktoren 3: In Ops.factor(y, mu) : -' nicht sinnvoll für Faktoren
asked Nov 24, 2019 alecxe 7.5k points
0 votes
1 answer 948 views
Problem : Suppose I have data.frame a And I am using following code m.fit <- lm(col2 ~ col3 * col4, na.action = na.exclude) Here the col2 has some NA values And col3,col4 have values less than 1. I am getting following error: Error in lm.fit ... because of the NAs. Problem with this warning is interpreted by every google results is pointing towards the NA values. Have I misinterpreted the error ?
asked Nov 22, 2019 peterlaw 6.9k points
1 vote
1 answer 1K views
Problem : I want to use the glmnet package on a dataset. I am trying to use cv.glmnet() to get the lambda value for the glmnet(). Please find below the dataset and error message: > head(t2) X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 1 ... in evaluating the argument 'x' in selecting a method for function 't': Error: invalid class 'NA' to dup_mMatrix_as_dgeMatrix Any suggestions regarding the solution?
asked Dec 14, 2019 alecxe 7.5k points
0 votes
1 answer 44 views
How to convert non-finite calues to integer?
asked Sep 22 Daniel Anderson 4k points