Professional Documents
Culture Documents
1 Basics
1. Consider the Rpop.txt file available on the course website. The file contains the
weighted number of monthly searches worldwide of the term ’download R’ in Google
between January 2004 and August 2019 (i.e. monthly searches divided by the maximum
monthly search observed between January 2004 and August 2019). The data can be
downloaded from Google Trends.
2. Consider the colors.txt file available on the course website. The file contains favorite
colors of ten children.
3. Consider the GirlsBoys2018.txt file available on the course website. The file contains
the number of girls and boys EDHEC students kiss during their first semester at
EDHEC.
(a) Read the data using the web address of the data.
(b) Draw a histogram of the data.
(c) Calculate the sample
i. minimum;
ii. maximum;
iii. mean;
iv. median;
v. standard deviation;
vi. size.
4. Consider the co2percap2017.txt file available on the course website. The file contains
the per capita CO2 emissions (in tonnes) in 2017 for 197 regions in the world (source:
https://ourworldindata.org/co2-and-other-greenhouse-gas-emissions).
(a) Read the data using the web address of the data.
(b) Draw a histogram.
1
2 FUNCTIONS
2 Functions
1. Download the data in GirlsBoys2018.txt from the course website. The data contains
the number of girls and boys EDHEC students kiss during their first semester at
EDHEC.
(a) Write a function sumof2 that calculates the sum of two user-specified numbers.
Using sumof2, calculate the total number of boys/girls the first two students in
the data kissed during their first semester at EDHEC.
(b) Write a function that calculates ln(x+1) for a user-specified x. Calculate ln(x∗ +1)
where x∗ is the number of girls/boys the first student kissed during his/her first
semester at EDHEC.
2. Write a function randnumb that generates a random number between 0 and x, where
x is a user-specified number. Use it to generate a random number between 0 and 10.
3. Download data in co2percap2017.txt containing per capita CO2 emissions (in tonnes)
in 2017 for 197 regions in the world. Save the data under the data vector co2 and draw
a histogram co20.25 .
4. Do the following exercises.
(a) Write a function fNormal that calculates
1 1 x−m 2
fNormal(x, m, s) = √ e− 2 ( s ) ,
2p|s|
where p = 3.141593 and x, m, s are user-specified numbers. Evaluate fNormal at
x = 50, m = 50, s = 10.
(b) Write a code that plots the function fNormal for m = 50 and s = 10 and various
x on the interval [1, 100]. To do so, create a data vector v of length 100:
v = (1, 2, . . . , 100) ,
and another data vector y:
y = (y1 , y2 , . . . , y100 ) ,
such that yi = fNormal(vi , m, s) for all i from 1 to 100 and m = 50 and s = 10.
Plot y against v.
2
3 CONTROL FLOW STATEMENTS
3. Download data in co2percap2017.txt containing per capita CO2 emissions (in tonnes)
in 2017 for 197 regions in the world. Save the data under the data vector co2. Write
a function score such that
’A’ if x < 2.5
’B’ if 2.5 ≤ x < 7.5
’C’ if 7.5 ≤ x < 12.5
score(x) =
’D’ if 12.5 ≤ x < 17.5
’E’ if 17.5 ≤ x < 25
’F’ if x ≥ 25
3
4 MATRICES AND DATA FRAMES
Evaluate the function at co2 of the 1st and the 150th regions in the data.
5. Write a function sumofn which, for a given integer n, calculates the sum of the first n
positive integers. Evaluate this function for n = 100.
6. Download the data in GirlsBoys2018.txt from the course website. The data contains
the number of girls and boys EDHEC students kiss during their first semester at
EDHEC.
(a) The value 1 is the most common value in the dataset. Write a code that calculates
the proportion of students who kissed 1 girl/boy during their first semester at
EDHEC using
i. for;
ii. while.
(b) Download the data in Beer2018.txt from the course website. The data con-
tains the weekly beer consumption (in litres) of the same EDHEC students as in
GirlsBoys2018.txt. The largest value in GirlsBoys2018.txt is 120 (and there
is only one such observation). Calculate the weekly beer consumption of that
student using
i. for;
ii. while.
4
4 MATRICES AND DATA FRAMES