Solution Review: Find the Highest Achiever
In this review, we give a detailed analysis of the solution to the problem of finding the highest achiever.
We'll cover the following...
Solution: Merging Data Frames
Explanation
The code starts executing from line number 47 when the function findTopper() is executed.
This function starts executing from line number 22 (main file handling code)
Steps Performed:
- Line number 23-25: Read all the subject files in the variables:
mathData,englishDataandscienceDatarespectively. These will also act as the data frames.
Remember, data fetched from a
.csvis already in the form of a data frame.
-
Line number 27-28: Merge the three data frames into one data frame. In the code snippet above, we have broken merging of the three data frames into two steps. First, merge
mathDataandenglishDataand save intempData. Then mergetempDataandscienceDatainfinalData. -
Now that we have all the data compiled in one data frame
finalData, we can begin performing our analysis on it. -
Line number 32-43: We use nested
forloop to iterate over the whole data frame. The outer loop:
for(student in 1:length(finalData))
keeps track of the rows/students. Since the value of length(finalData) is we are basically executing loop from student to student .
The inner loop:
for(i in 2:ncol(finalData))
iterates over all the columns (math, english, science). Notice, we iterate from column to ncol(finalData) because the column is just names of students.
Then we add the marks of all subjects of each student. The loop can be illustrated as follows:
The findIndexWithMaxNum() returns the index of the largest/maximum number in an array. We have created this as a helper function to find the index of the maximum element in the result vector.
In the next chapter, we discuss installing and loading packages in R language.