. Advertisement .
..3..
. Advertisement .
..4..
I have the following r code, but I do not know how to find the correct result. Why has this problem occurred, and how can it be solved? This is the code I am running:
#create data frame df
<- data.frame(var1=c(2, 4, 4, 6, 7, 8, 8, 9, 9, 12),
var2=c(12, 14, 14, 8, 8, 15, 16, 9, 9, 11),
var3=c(22, NA, 23, 24, 28, 23, 19, 16, 12, 15))
row.names(df) <- LETTERS[1:10]
#view data frame
df
var1 var2 var3
A 2 12 22
B 4 14 NA
C 4 14 23
D 6 8 24
E 7 8 28
F 8 15 23
G 8 16 19
H 9 9 16
I 9 9 12
J 12 11 15
This is the error text I receive:
#attempt to perform k-means clustering with k = 3 clusters
km <- kmeans(df, centers = 3)
Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1)
The cause: The reason is that when you attempt to perform k-means clustering in R but the data frame you’re using misses one or more values.
Solution: The simpliest way to fix this error is to use the na.omit() function to remove rows with missing values from the data frame:
This error can also be caused by non-numerical values in the table.