According to the following steps,you can run my code successfully.
Step1: After you download the UCI HAR dataset,you should find out the following files from the UCI HAR dataset: X_test.txt, X_train.txt, y_test.txt, y_train.txt, subject_test.txt, subject_train.txt, features.txt and activity_labels.txt.
Step2: Copy the aforementioned files into your own working directory
Step3: Copy my code into your own run_analysis.R script
Step4: Source and run the run_analysis.R script generated in Step3,you will get the final result.