Reputation: 7
Quitting from lines 12-26 (6,4-PresentationRMARKDOWN.Rmd) Error in do_one(nmeth) : NA/NaN/Inf in foreign function call (arg 1) Calls: ... withCallingHandlers -> withVisible -> eval -> eval -> kmeans -> do_one
Execution halted
pwvspl <- function(dataselected){
dataselected <- iris
data(dataselected)
set.seed(8593)
dataselected
dataselected$species <- NULL
(kmeans.result <- kmeans(dataselected,3))
table(dataselected$Species, kmeans.result$cluster)
plot(dataselected[c("Petal.Length","Petal.Width")], col = kmeans.result$cluster)
points(kmeans.result$centers[c("Petal.Length","Petal.Width")],col=1:3, pch=3, cex=2)
data(dataselected)
data_for_clustering <-dataselected[,-5]
clusters_dataselected <- kmeans(data_for_clustering, centers=3)
plotcluster(data_for_clustering,clusters_dataselected$cluster)
clusplot(data_for_clustering,clusters_dataselected$cluster, color=TRUE, shade=TRUE, main = "Petal Width vs. Petal Length", xlab="Petal Length", ylab="Petal Width")
}
I am unsure what is causing this error. How do I resolve this error?
Upvotes: 0
Views: 276
Reputation:
Instead of dataselected$species <- NULL
change this line to just converting the species factors to numeric:
dataselected$Species <- as.numeric(dataselected$Species)
Upvotes: 1
Reputation: 316
Welcome to SO.
The traceback shows
3. do_one(nmeth)
2. kmeans(dataselected, 3)
1. pwvspl()
which means the error is inside the kmeans
function. To be precise, the error is in function do_one
, which is called by kmeans
.
The help page for kmeans
says
numeric matrix of data, or an object that can be coerced to such a matrix (such as a numeric vector or a data frame with all numeric columns).
But the iris
dataframe has nonnumeric columns (whose values become NA
when the function tries to coerce them to numeric).
That's the cause of the error.
Upvotes: 0