error in knn function

I have a function error "Error in knn (train = iris_train, test = iris_test, cl = iris_train_target,:
no missing values are allowed."
and I don't know how to fix it
example1 is iris in a variable a

library(GLDEX)
library(tidyverse)
library(class)
data(iris)
a <- iris


contaNa <- which.na(a$Species)

contaNa
gp <- runif(nrow(a))
gp
a <- a[order(gp),]
class(a)
str(a)
a
summary(a)
normalize <- function(x){
  (x-min(x, na.rm = T))/(max(x,na.rm = T)-min(x,na.rm = T))
}
iris_n <- as.data.frame(lapply(a[,c(1,2,3,4)], normalize))
str(iris_n)
summary(iris_n)

iris_train <- iris_n[1:150,]
iris_test <- iris_n[1:150,] 
iris_train_target <- a[1:150,5]
iris_test_targer <- a[1:150,5]

require(class)

ml <- knn(train = iris_train, test = iris_test, cl = iris_train_target, k = 3)
ml#

Hi, hope I don't cause offence, but as a general comment your code seems confused in that your load iris data, but dont use it, rather you load Ejempl1.csv (which we don't know the contents) and go on to name the creation from this with iris flavoured name... its a bit odd.
anyway.
I see that you test for na in a$Speciea, but did you test for the other variables you include ?

lapply(a[,c(1,2,3,4)]

also when you use mutate() with replace() you arent assigning or piping back, so aside from printing an altered biew of the contents of 'a', 'a' will not have changed.

finally, you make use of summary(), but you don't share with us the printed results, so we cant tell from looking whether it tells if there are NA's or not...

1 Like

thank you accommodate the code the error that comes out is in the last line in the knn function

Yes. According to the error, you are giving missing values.
Can you demonstrate that you are not?

I've researched but they all show the same,