Solution Review: Find Mean and Median
Explore how to read data from a text file into R, store it in a vector, and calculate the mean and median using R functions. This lesson guides you through practical file handling and basic statistical operations in R.
We'll cover the following...
Solution: Input and Output from .txt File
Explanation
This solution is a bit tricky, but don’t worry we will break it down for you.
Let’s begin from the driver code (line number 29).
Steps Performed:
-
Line number 30 - 32: Open the file and fetch all the lines in a variable.
-
Line number 34 - 38: Populate a vector to store inputs from the file.
-
Line number 40: Create a
resultvector. -
Line number 41 - 45: Find
meanandmedianusing their functions and store the output in theresultvector. -
Line number 47: Write the
resultvector onoutput/outData.txtfile.
mean() and median() functions are simple. They just take a vector, iterate over all the elements, and output the specific answer.
In the next lesson, we have another challenge for you to solve.