MTH208: Worksheet 2
Introduction to R cont…
We have familiarized ourselves with some starter exercises in R. Now let’s try to do some visual and
exploratory analyses
1. Write a function in R to find 𝑛!. This function should have an argument n that can be used
to call the function for a given integer. Verify your answer with factorial() function.
2. Euler’s number, 𝑒, is accessed through exp(1) in R. Similarly 𝑒3 is exp(3). It is well known
that
1 𝑛
𝑒 = lim (1 + )
𝑛→∞ 𝑛
Write a function in R that calculates the right hand side (without the limit) for a user-given
value of 𝑛.
3. Load the seating dataset:
seat <- [Link]("[Link]
Write R code to find your assigned seat using your roll number as reference.
4. The [Link] dataset is also shared in the repository folder. Using [Link]() function,
load the locally downloaded [Link] into your R session. You will need to make sure
you set the working directory to the folder where the csv file is.
5. Recall the seating chart for this course
seat <- [Link]("[Link]
MSc students have 9-digit roll numbers and BS students have 6-digit roll numbers. Write R
code to calculate the number of MSc students enrolled in this course, and the number of BS
students enrolled in this course.
1
6. An “all-rounder” in cricket is a player who performs well in both batting and bowling. The
dataset [Link] in your repository contains the batting and bowling ODI aver-
ages of selected male players. Load the dataset using the following command (remember that
your working directory should be the directory that contains this file).
cricket <- [Link]("[Link]")
(A high batting average is good, a high bowling average is bad.) Let’s say a decent batter
is someone with a batting average higher than 25 and a decent bowler is someone with a
bowling average below 40.
1. Create a sub-dataset of all all-rounders using the above criterion.
2. Which team has the most all-rounders?
3. Which team has the least all-rounders.
7. The plot() function can be used to make a variety of plots in R. Do ?plot on the console to
learn how the syntax for plots works. Reproduce the following 𝑦 = 𝑥 plot given below.
Y = X Plot
10
8
6
y
4
2
0
0 2 4 6 8 10
8. For 𝑛 = 1, … , 1000, make a plot of 𝑛 versus 𝑓(𝑛) where
1 𝑛
𝑓(𝑛) = (1 + )
𝑛
Using abline(), draw a horizontal line, in red, at the value 𝑒.