0

I am trying to build KNN model

 library(class)

> predictedknn<-knn(train=social,test=socialtest,cl=social,k=3)

Error in knn(train = social, test = socialtest, cl = social, k = 3) : 'train' and 'class' have different lengths

Lengths are:

  • social = 280 objs with 3 variables
  • socialtest =120 onjs with 3 variables
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 21 '21 at 16:52
  • According to the manual page for `knn` (see `help(knn)`), `train=` is a matrix or data frame and `cl=` is a factor of true classifications. You are trying to use the data frame `social` in both places. Look at the Examples on the manual page. – dcarlson Sep 21 '21 at 21:05

0 Answers0