simple 1NN

I'm confused here as i'm unable to figure out why code is not working as expected on entire test data set. Same code works when I use it to predict value of 1 test data. 
I think what you want here is:
train_index <- sample(nrow(dataframe), .80*nrow(dataframe))