Q:

Perform knn classification with all odd values of k between 1 and 21 in R

I am trying to perform knn with the iris dataset with multiple odd values of k between 1 - 21 (inclusive).  Is the right way to combine all knn and still receive the right results?  Also, how would I create a confusion matrix of the results and check for accuracy?  
I am getting the following errors when creating the confusion matrix and checking accuracy...

ERROR 1: Error in table(knn_class, train.labels) : 
    all arguments must have the same length
ERROR 2: In ==.default(knn_class, train.labels) :
    longer object length is not a multiple of shorter object length

as I am performing knn with multiple k, train.labels is less than knn_class but I can't understand how to resolve.  
Here is my code so far...
library(class)
data(iris)

# Generate a random sample that is 2/3 of the total number of rows in dataset. 
ran <- sample(2, nrow(iris), replace=TRUE, prob=c(0.80, 0.20))
ran

# Compose training labels
train.labels <- iris[ran==1, 5]

# Compose test labels
test.labels <- iris[ran==2, 5]

# Compose training set. 
training <- iris[ran==1, 1:4]
data_train <- cbind(train.labels,training)

# Compose test set. 
test <- iris[ran==2, 1:4]
data_test <- cbind(test.labels,test)

k_values <- seq(from=1, to=21, by=2)

knn_class <- sapply(k_values, function(k) {
  knn(train = training, test = test, cl = train.labels, k=k)
})
colnames(knn_class) <- c(1,  3,  5,  7,  9, 11, 13, 15, 17, 19, 21)
summary(knn_class)
          1               3               5               7               9               11              13    
 setosa    :13   setosa    :13   setosa    :13   setosa    :13   setosa    :13   setosa    :13   setosa    :13  
 versicolor:11   versicolor:10   versicolor: 9   versicolor: 9   versicolor: 9   versicolor:10   versicolor:10  
 virginica : 9   virginica :10   virginica :11   virginica :11   virginica :11   virginica :10   virginica :10  
          15              17              19              21    
 setosa    :13   setosa    :13   setosa    :13   setosa    :13  
 versicolor:11   versicolor:11   versicolor:10   versicolor:11  
 virginica : 9   virginica : 9   virginica :10   virginica : 9  

# Checking Accuracy of knn. 
# Error 1 here. 
con_mat <- confusionMatrix(table(knn_class, train.labels))

# Error 2 here. 
mean(knn_class == train.labels) # success rate

Any help would be appreciated. 

A:

The first problem is that you are using the training labels to compare your results, this gives you different sizes.  The KNN class is a matrix of 25 rows to 11 columns.  You need to compare your results with the test labels. 
Another mistake is that you need to create your confusion matrix and take the accuracy for each value of K. This code solve your problem:
accuracy <- c()
con_mat <- list()
for(i in 1:ncol(knn_class)) #iterating over columns
{
  con_mat[[i]] <- table(knn_class[,i], test.labels) #geting the confusion matrix
  accuracy = c(accuracy,mean(knn_class[,i] == test.labels)) #geting the accuracy 
}

