首页 > 解决方案 > 如何解决 NA/NaN/Inf 执行停止错误?

问题描述

从第 12-26 行退出 (6,4-PresentationRMARKDOWN.Rmd) do_one(nmeth) 中的错误:外部函数调用中的 NA/NaN/Inf (arg 1) 调用:... withCallingHandlers -> withVisible -> eval -> eval -> kmeans -> do_one

执行停止

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")
}

我不确定是什么导致了这个错误。如何解决此错误?

标签: rfunctionsyntax-error

解决方案


而不是dataselected$species <- NULL将此行更改为仅将物种因子转换为数字:

dataselected$Species <- as.numeric(dataselected$Species)

在此处输入图像描述


推荐阅读