Error in `[.default`(data, , all.vars(Terms), drop = FALSE) : incorrect number of dimensions



Hi all,
I have been facing an error in R and I am finding it difficult to find a solution for the same. please help me out.
the code and error as follows

Error in [.data.frame(data, , all.vars(Terms), drop = FALSE) :
undefined columns selected

now the crux of the problem is: I had a test on knn with k=1,2,3; and it was perfect, without any error. I was interested in cross-validation and when I did the same. I received this error above.

Additional information: I have separated ytrain data; ytrain is a factor with 2 levels. when I check nrow(ytrain) it say NULL. Later I tried to change it to and retried the same. yet it is not working. help me out