Steps 1 to 3 load the necessary packages and read the data file.
Step 4 creates three partitions (50 %, 25 %, and 25 %). We set the random seed to enable you to match your results with those that we display. Refer to the Creating random data partitions recipe in Chapter 2, What's in There? - Exploratory Data Analysis, for information on data partitioning.
Step 5 uses the knn function to generate predictions with k=1. It uses only the standardized values of the predictor variables and hence specifies train[,4:5] and val[,4:5].
Step 6 generates the error matrix for k=1.