0

When I execute this code, I get the next wrong about coercionNAs:

data_test_pred <- knn(train = data_train, test = data_test,
                      cl = data_train_labels, k = 9)

NAs introduced by coercionNAs introduced by coercionError in knn(train = data_train, test = data_test, cl = data_train_labels,  : 
  NA/NaN/Inf in foreign function call (arg 6)

The variable of data_train_labels is two Levels

Factor w/ 2 levels "Prom","No prom": 1 1 1 1 1 1 1 1
Raq
  • 75
  • 1
  • 6
  • Usually `NAs introduced by coercion` is a warning given when a function is expecting a data type and but you input another. In this case maybe the labels should be `character`? – Leonardo Viotti Mar 03 '21 at 23:24
  • But it would be really useful if you could add a reproducible example including loading the packages and mock data. – Leonardo Viotti Mar 03 '21 at 23:24
  • Have you checked this post? https://stackoverflow.com/questions/16874038/error-with-knn-function – Ronak Shah Mar 04 '21 at 04:04

1 Answers1

0

In this situation I guess this occurs due to the transformation of character variables to numeric.

By transforming character to numeric you will get NA's. You should set your character variables to factor and then to numeric variables.

Learned here:

I get the error NAs introduced by coercionNAs when trying to run kNN in R?

Otherwise please post the head of your data.

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thanks you for your answer but when I change the value of numeric I get: Prom → 1 No Prom → 2 But I would like get: Prom → 1 No Prom → 0 – Raq Mar 04 '21 at 18:48