MAS1802 Practical 3: Tues 24th March (drop-in Thurs 26th March; deadline 3pm Friday 27th March)
Practical 3: Functions and loops
In the questions below, the important part is understanding what’s going on rather than just typing the
R commands. Make sure you answer the questions under each piece of R code. If you can’t answer the
question, then ask! When trying out the code below, do not include the plus symbols!
1 Practice questions
1. Default arguments
> Fun1 = function(x=10) {
+ return(x)
+ }
> Fun2 = function(x) {
+ return(x)
+ }
a) Why does typing Fun1() work, but typing Fun2() give an error message?
b) Change Fun1 so that it returns
p
x.
2. Basic functions
> v = 5
> Fun3 = function() {
+ v = 0
+ return(v)
+ }
> Fun3()
> v
a) Why does line 7 above give 5 and not zero?
b) Delete line 3 in the above listing. Now change the function to allow v to be passed as an
argument, i.e. we can run Fun3(5). Call this function to make sure it works.
c) Now make the argument have a default value of 0.
3. This question makes sure that you can save a file and upload it to NESS. It is crucial you can do
this for the assignment questions later on. Suppose you wanted to save Fun1 and Fun2, from the
exercises above, for submission as part of an assignment. In the text editor window (top left), make
sure you delete all code except that for Fun1 and Fun2. Now click on the disk icon at the top of
the text editor window and then save the file as “practice”. This will automatically save the file as
an R file, called “practice.R”. You will now upload this file to NESS using the following steps – first
of all, you must log-in to NESS. Then...
• Select “MAS1802” at the top of the screen.
• Select “Submit” from the drop-down “Coursework” menu at the left-hand-side.
• Choose “Practice” from the box which says “Please select an exercise”.
• “Browse” for your saved R file in the “Files to be submitted box”.
• Click “Submit your work” at the bottom of the screen.
Dr Lee Fawcett – lee.fawcett@ncl.ac.uk 1
MAS1802 Practical 3: Tues 24th March (drop-in Thurs 26th March; deadline 3pm Friday 27th March)
4. if statements
> Fun4 = function(x) {
+ if(x==5) {
+ y = 0
+ } else {
+ y = 1
+ }
+ return(y)
+ }
a) Call Fun4 a few times with different values of x.
b) Change Fun4 so that it returns 0, if x is greater than 5.
c) Change Fun4 so that it returns 0, if x is greater than or equal to 5.
d) Change Fun4 so that it:
• returns 1 if x is positive;
• returns –1 if x is negative;
• returns 0 if x is zero.
e) Suppose that x is a vector. Change the function so that it returns 0 if mean(x) is greater than
0.
f) Change Fun4 so that if x is positive, it returns
p
x.
5. for loops
> total = 0
> for(i in 1:5) {
+ total = total + i
+ }
> total
The for loop above calculates
5∑
i=1
i .
a) What is the final value of total in the above piece of code?
b) Change the code above to calculate the following summations:
(i)
20∑
i=1
(i +1) (ii)
15∑
j=−10
j (iii)
10∑
j=−10
exp( j 1/ j ) (iv) 0,3,6,9, . . . ,24 (v)
20∑
j=0
1
2 j
.
6. More for loops
> a = 2
> total = 0
> for(blob in a:5) {
+ total = total + blob
+ }
a) Delete line 1 above and put the code in a function called Fun5, where a is passed as an
argument, i.e. we can call Fun5(1)
Dr Lee Fawcett – lee.fawcett@ncl.ac.uk 2
MAS1802 Practical 3: Tues 24th March (drop-in Thurs 26th March; deadline 3pm Friday 27th March)
b) Alter the code so that the for loop goes from a to b, rather than a to 5. Allow b to be passed
as an argument, i.e. so we can call Fun5(1,5).
c) Change Fun5 so that it has default arguments of a=1 and b=10.
7. The following function calculates the sum of a vector:
> Fun6 = function(x) {
+ n = length(x)
+ total = 0
+ for(i in 1:n) {
+ total = total + x[i]
+ }
+ return(total)
+ }
Alter Fun6 to calculate the mean of the vector x. Do not use the built-in mean function. Test your
function.
The assignment questions are on the next page
Dr Lee Fawcett – lee.fawcett@ncl.ac.uk 3
MAS1802 Practical 3: Tues 24th March (drop-in Thurs 26th March; deadline 3pm Friday 27th March)
2 Assignment
Preliminaries
For the questions below you will need to save your R code in a single file called
practical3.R
You will upload this file to NESS in the same way you uploaded the practice file to NESS in question 3.
• Choose ‘MAS1802” from the module selection boxes at the top of the page.
• From the “Coursework” menu down the left-hand-side, choose “Submit”.
• From the list in the box in the middle of the screen, select “Computer practical 3”.
• Browse for your R file in the “Files to be submitted box”.
• Click “Submit your work” at the bottom of the page.
Warning: If you save your file incorrectly, you will receive zero marks for this practical!
Questions
In this section do not use the built-in R functions sum, mean, median, sd, var or summary.
1. Write a function called MySD that calculates the sample standard deviation, i.e.√
1
n−1
n∑
i=1
(xi − x¯)2.
This function should have a single argument x.
2. Write a function called MyDifference that takes two vector arguments, x and y, and calculates
the differences in means, i.e.
x¯− y¯ .
3. Write a function called MyMedian. This function has a single argument x and returns the median
value of a vector. You may use the built-in function sort to help you.
4. Write a function called IsEven. This function has a single argument x and returns TRUE if the
number is even and FALSE if the number if odd.
What should (and shouldn’t) be in your file
1. Your file should only include functions. It can include additional helper functions, such as MyMean
(Hint: see question 7)
2. Your file should not include function checks
3. Any statements – e.g. function descriptions – should be included after the # symbol
Dr Lee Fawcett – lee.fawcett@ncl.ac.uk 4  