You are on page 1of 5

1 BASICS

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.

(a) Save the file on your computer.


(b) Read the data using the location of the data on your computer.
(c) Read the data using the web address of the data.
(d) Plot the data.

2. Consider the colors.txt file available on the course website. The file contains favorite
colors of ten children.

(a) Save the file on your computer.


(b) Read the data using the web address of the data.
(c) Print the data.
(d) Provide the favorite color of the third child in the data.

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

(c) Calculate the sample


i. mean;
ii. median;
iii. lower quartile (0.25th quantile of the data);
iv. upper quartile (0.75th quantile of the data).

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

5. Write a function fln that calculates


ln(x)
fln(x, a) = −
a
for a user-specified x ∈ (0, 1) and a > 0. Create a data vector of length 1000:
v = (v1 , v2 , . . . , v1000 )
where vi is a randomly generated number between 0 and 1. Create another vector
y = (y1 , y2 , . . . , y1000 )
such that yi = fln(vi , 1) for all i from 1 to 1000. Draw a histogram of y.

3 Control flow statements


1. Do the following exercises.
(a) Write a function mysign such that

’Negative number’ if x < 0

mysign(x) = ’Zero’ if x = 0

’Positive number’ if x > 0

where x is a user-specified number.


(b) Evaluate the function at -1, 0, 1, respectively.
2. Do the following exercises.

(a) Write a function sqrt5 such that


(√
x−5 if x ≥ 5
sqrt5(x) =
’Please choose a number greater than 5.’ otherwise
for a user-specified x.
(b) Evaluate the function at 4, 5, 6, respectively.

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.

4. Write a function grade such that





 ’A’ if 17 ≤ x ≤ 20
if 14 ≤ x < 17



 ’B’

’C’ if 11 ≤ x < 14



grade(x) = ’D’ if 8 ≤ x < 11

’E’ if 5 ≤ x < 8







 ’F’ if 0 ≤ x < 5

’Please give a grade between 0 and 20.’ otherwise

Evaluate the function at -1, 0, 5, 18, 21, respectively.

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 Matrices and data frames


1. Write a code that creates the matrix
 
1 2 3
4 5 6
 
7 8 9
10 11 12

4
4 MATRICES AND DATA FRAMES

2. Write a code that creates the matrix


 
1 5 9
2 6 10
 
3 7 11
4 8 12
3. Write a code that creates the matrix
 
Blue D
Yellow B 
Red A
4. Write a code that creates the matrix
 
Blue 1
Yellow 2
Red 3
5. Let  
1 2 3
A = 4 5 6
7 8 9
Display
(a) the element in the second row and first column;
(b) the last two elements in the second column;
(c) the first and the third elements in the second column;
(d) the second column.
6. The data co2percapita.csv on the course website contains per capita CO2 emissions
(in tonnes) between 1800 and 2017, for 197 regions in the world.
(a) Download the data. Assign A to the data frame.
(b) Redo (a) using co2percapita.txt .
(c) Display the first 10 rows of A .
(d) Display Entity for Year value equal to 1800 .
(e) Plot the per capita CO2 emissions between 1800 and 2017 for:
i. United States;
ii. France;
iii. Spain;
iv. Hungary.
(f) Create a data frame B such that the first column contains the four countries in
the previous exercise and the second column contains the CO2 emissions in 2017.
Name the first and second columns Country and Co2, respectively.

You might also like