title: Error in knn 'train' and 'class' have different lengths
body: <p>I'm trying to use the knn function (from the class package) on my dataset. It has 5 columns of features, and the 6th is what I want to be able to predict. I'm doing a 70/30 split.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&gt; ind &lt;- createDataPartition(CSD$Caesarian, p=0.70, list=FALSE)
  &gt; csd_train &lt;- CSD[ ind,]
  &gt; csd_test &lt;- CSD[-ind,]
  &gt; c1 &lt;- CSD[1:6,-c(1,2,3,4,5)]
  &gt; knn(train, test, c1, k=2, prob=TRUE)
  </code></pre>
  
  <p>But I'm getting this error.</p>
  
  <pre><code>Error in knn(train, test, c1, k = 2, prob = TRUE) : 
    'train' and 'class' have different lengths
  </code></pre>
  
  <p>I looked at other threads and trying their suggested solutions (<a href="https://stackoverflow.com/questions/16276388/knn-in-r-train-and-class-have-different-lengths">KNN in R: &#39;train and class have different lengths&#39;?</a>)</p>
  
  <p>and tried the following, but I'm still getting errors</p>
  
  <pre><code>&gt; c1 = as.factor(c1)
  &gt; dim(csd_train)
  [1] 57  6
  &gt; dim(csd_test)
  [1] 23  6
  &gt; length(c1)
  [1] 6
  &gt; knn(train, test, c1, k=2, prob=TRUE)
  Error in knn(train, test, c1, k = 2, prob = TRUE) : 
    'train' and 'class' have different lengths
  </code></pre>
  
  <p>I also tried this, and still getting an error.</p>
  
  <pre><code>&gt; c1 = as.factor(CSD[['Caesarian']])
  &gt; knn(train, test, c1, k=2, prob=TRUE)
  Error in knn(train, test, c1, k = 2, prob = TRUE) : 
    'train' and 'class' have different lengths
  </code></pre>
  
  <p>I'm lost as to how to fix this.</p>
  
  <p>Here's a sample of my data if that helps:</p>
  
  <pre><code>&gt; dput(head(CSD))
  structure(list(Age = c(22L, 26L, 26L, 28L, 22L, 26L), Delivery.NO = c(1L, 
  2L, 2L, 1L, 2L, 1L), Delivery.NO.1 = c(1L, 1L, 0L, 1L, 1L, 0L
  ), BP = c(2L, 1L, 1L, 2L, 1L, 0L), Heart.Problem = c(1L, 1L, 
  1L, 1L, 1L, 1L), Caesarian = structure(c(1L, 2L, 1L, 1L, 2L, 
  1L), .Label = c("N", "Y"), class = "factor")), .Names = c("Age", 
  "Delivery.NO", "Delivery.NO.1", "BP", "Heart.Problem", "Caesarian"
  ), row.names = c(NA, 6L), class = "data.frame")
  </code></pre>
  
  <p><strong><em>EDIT</em></strong>
  I did </p>
  
  <pre><code>c1 &lt;- csd_train[, 6]
  </code></pre>
  
  <p>and the length(c1) is now 57, which is good. However, when I run the knn line, I'm now getting this new error:</p>
  
  <pre><code>Error in knn(csd_train, csd_test, c1, k = 2, prob = TRUE) : NA/NaN/Inf in `foreign function call (arg 6) In addition: Warning messages: 1: In` `knn(csd_train, csd_test, c1, k = 2, prob = TRUE) : NAs introduced by coercion 2:` `In knn(csd_train, csd_test, c1, k = 2, prob = TRUE) : NAs introduced by coercion`
  </code></pre>
  
  <p>All of my predictor  variables are numeric, and there are no missing values.</p>