• Register
1 vote
724 views

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

Please log in or register to answer this question.

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)
km_cluster
km_cluster$withinss
km_cluster$tot.withinss/km_cluster$betweenss

Hope my above solution helped you in fixing your issues.

8 4
5,680 points

Related questions

0 votes
1 answer 200 views
200 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 2K views
2K 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 1.3K views
1.3K 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 1.5K views
1.5K 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 385 views
385 views
How to convert non-finite calues to integer?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 19 views
19 views
Problem: np.nan is an invalid document, expected byte or unicode string.
asked 4 days ago charles mathews 3.8k points
1 vote
2 answers 288 views
288 views
Problem : I am very new to R. Currently I want to split the column of my data frame (menuA) is as shown below Column1 1|3|4|5 4|5|6|7 I was trying to split my numbers in above columnA so I coded it as shown below. menuA <- data.frame (do.call('rbind', ... 120"),  :   number of columns of result is not a multiple of vector length (arg 1) Please let me know if all my data is separated properly?
asked Apr 22, 2020 stewart 4k points
0 votes
1 answer 115 views
0 votes
1 answer 576 views
576 views
Problem: I have only the fundamental knowledge related to R. I want to simulate the very basic moving average process in R which is as shown below for your reference: x_t = Z_(t-1)-0.4Z_(t-2)+Z_t. Please find below my code in R: z = rnorm(500, 0, 1) x ... was unable to find the exact solution for my above error. I am looking for some help from R expert if possible to fix my above R related error.
asked Aug 12, 2020 Raphael Pacheco 4.9k points
0 votes
2 answers 510 views
510 views
Problem : I am trying to do data manipulation using R. I am trying to import the file which is having headings and also numbers in the multiple columns. I am trying to divide my values in the one column by the other column and then trying to determine the highest values. But ... below error &ldquo;Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 'x' must be atomic&rdquo;.
asked Feb 17, 2020 mphil 2.3k points