Chapter 3 Lab 1 - 02/03/2022
In this lecture we will learn the basic commands for programming with R.
We will work in RStudio.
Place your cursor in the Console and type the following code or any other mathematical operation:
5+(-2/0.5)
## [1] 1
R
can work like a calculator and you can compute whatever quantity you need. The point is that if you close RStudio, you will lose all your code. To avoid this, I suggest to use scripts (i.e. text files where you can write and run your code). To open a new one, use the RStudio menu: File - New File - RScript. Write all your code in your script(s) and use ctrl/cmd + enter
to run the code in the Console. Save the script by using the menu: File - Save as…. The extension of a script is .R
(choose the name you prefer). In the future you will be able to close and re-open your script with RStudio without losing anything.
3.1 Built-in functions
Before starting using a function, it is good pratice to visualise the help page (by running ?nameofthefunction
), in order to understand how the function is defined and which are its arguments and the default settings.
To run a function (and get the results) type the name of the function followed by round parentheses, inside which you specify the arguments of the functions (the inputs).
For example to compute the logarithm of a number the function log
is used. Its help page can be obtained by
?log
Note that the function is characterized by two arguments: x
(number/vector for which the log is computed) and base
(the base of the log function, by default it’s the natural log).
Let’s compute the log as follows:
log(x = 5 , base = 10) #log of 5 with base 10
## [1] 0.69897
log(x = 5) #natural log
## [1] 1.609438
Argument names can be omitted. In this case it is very important to be careful about the order of the arguments passed to the function (the order is given by the function definition, check the help).
log(5, 10)
## [1] 0.69897
log(10, 5) #warning: this is log of 10 with base 5
## [1] 1.430677
3.2 Objects and vectors
In R
it is possible to create objects by using the assignment operator =
(it is also possible to use <-
). As argument name you can choose any name you prefer; however, it’s better to use short and meaningful names. The following code assigns the number -1.5 to the object named x
(have a look to the top right panel!):
= -1.5 x
A vector of number is created using the c
function (concatenate). For example the following code is used to create a vector with 4 numbers. The vector is saved in an object named y
:
= c(-1.5, 99, log(15), 6.7) y
To visualize the values of y
just run the object name (remember that R is case-sensitive so that y
is different from Y
):
y
## [1] -1.50000 99.00000 2.70805 6.70000
The vector length is given by
length(y)
## [1] 4
It is possible to compute operations with vector. For example, the following code
+ 4 y
## [1] 2.50000 103.00000 6.70805 10.70000
add 4 to all the elements of y
. Note that R executes operations with vector element-wise (element by element singularly). The same element-wise approach is adopted also for computing operations involving two vectors of the same length:
# create another object z
= c(6.89, -10, 5.5, sqrt(log(5)))
z z
## [1] 6.890000 -10.000000 5.500000 1.268636
# operations with 2 vectors
- z y
## [1] -8.390000 109.000000 -2.791950 5.431364
To select elements from a vector we use squared parentheses. For example, to retrieve the second element the following code is used, where inside the parentheses the position of the element to be select is specified:
2] y[
## [1] 99
To select more than one element, a vector of positions is provided:
c(1,4)] #select the first and forth element y[
## [1] -1.5 6.7
If you need to select the first four element of y
you can proceed as follows:
c(1,2,3,4)] y[
## [1] -1.50000 99.00000 2.70805 6.70000
or by using the following shorter code where 1:4
generates a regular sequence of integers from 1 to 4:
1:4
## [1] 1 2 3 4
1:4] y[
## [1] -1.50000 99.00000 2.70805 6.70000
To replace values (after having selected them) we use the assignment operator =
:
# select the second element and replace it with 4
2] = 4
y[ y
## [1] -1.50000 4.00000 2.70805 6.70000
It is also possible to summarize the values in a vector by using summary statistics function such as the
sum
or the mean
applied to y
or any function of it:
sum(y) #sum of the elements of y
## [1] 11.90805
mean(y) #mean of the values of y
## [1] 2.977013
sum(y) / length(y) #another way for computing the mean
## [1] 2.977013
mean((y^3)+4) #first the operation ^3+4 is computed and then the sum
## [1] 99.3119
There are also other summary functions such as, for example, median
, quantile
, min
and max
.
It is also possible to compute logical operation whose result is TRUE if the condition is met or FALSE otherwise. For example:
>= 0 z
## [1] TRUE FALSE TRUE TRUE
gives a vector of TRUE/FALSE according to the condition ‘bigger than or equal to 5’ applied to each element of y
. Summary statistics can be applied also to vector of logical values, in this case TRUE is considered as 1 and FALSE as 0.
sum(z >= 0) #number of values >= 0
## [1] 3
mean(z >= 0) #proportion of values >= 0
## [1] 0.75
mean(z >= 0)*100 #% of values >= 0
## [1] 75
The following table lists all the logical operators available in R.
Operator in R | Description |
---|---|
<= >= | lower/bigger than or equal |
< > | lower/bigger |
== | exactly equal to |
!= | different from |
& | intersection (and) |
| (vertical bar) |
union (or) |
By using logical operator it is possible to select/replace elements in a vector by setting a condition. In this case it is not necessary to specify the positions in the vector of the elements to be selected/replaced. R will consider only the elements for which the condition is met:
# substitute the positive numbers of z with 0
> 0] = 0
z[z z
## [1] 0 -10 0 0
Let’s create now a new vector object called z2
which contains all the element of z
different from -10. In this case the condition which is tested is z == 10
which returns
== -10 z
## [1] FALSE TRUE FALSE FALSE
but we are instead interested in the complementary condition which can be obtained by using the exclamation mark in two possible ways:
! (z == -10)
## [1] TRUE FALSE TRUE TRUE
!= -10 z
## [1] TRUE FALSE TRUE TRUE
Finally, we can do the selection and create z2
:
= z[! z == -10]
z2 z2
## [1] 0 0 0
3.3 Random number generation from the Normal distribution
An example of random generation is given by the random experiment that consists in tossing a coin four times (T=tail, H=head). One possible outcome is {T, H, H, T, T}, while {H, H, H, T, H} is another possible random sequence. All the random sequence of Head and Tail are simulated from the distribution which assigns probability 0.5 to Head and probability 0.5 to Tail. We will proceed similarly by simulating values from the Normal distribution. In this case the task is more complex and we resort to a specific R function named rnorm
(r...
stands for random).
Consider for example that \(X\) is the travelling commuting time to get to the university. We assume that \(X\) follows a Normal distribution with mean 30 minutes and standard deviation 2 minutes. or simulating randomly 4 numbers from \(X\), we use the following code:
rnorm(4, mean=10, sd=1)
## [1] 11.853035 9.822154 10.947686 9.399162
This code returns 4 different number every time you run it. In order to get the same data, for reproducibility purposes, it is necessary to set the seed (i.e. to set the starting point of the algorithm which generates the random numbers). To do this the set.seed
function is used; it takes as input an integer positive number (5 in the following example):
set.seed(5)
= rnorm(4, mean=30, sd=2)
x x
## [1] 28.31829 32.76872 27.48902 30.14029
The 4 numbers are saved in an object named x
. By setting the seed we are able to reproduce always the same sequence of random numbers (which is the same for all of us).
We now simulate 1000 values from the same distribution \(X\), we will visualize the first simulated data with the command head
, and plot the empirical distribution by using the histogram and overlapping to it the KDE:
set.seed(5)
= rnorm(1000, mean=30, sd=2)
x head(x)
## [1] 28.31829 32.76872 27.48902 30.14029 33.42288 28.79418
hist(x)
It is possible to compute the percentage of values lower than 25 minutes (left tail) or bigger than 35 minutes (right tail):
mean(x > 35 | x < 25) * 100
## [1] 1.4
And this is the code for substituting the values in the tails with the mean of the simulated distribution:
> 35 | x < 25] = mean(x) x[x
3.4 Exercises Lab 1
3.4.1 Exercise 1
Compute \(\exp(3-\frac{4}{5})+\frac{\sqrt{3+2^5}}{4-7\cdot \log(10)}\)
Create the vector named
x
which contains the following values \((10, log(0.2), 6/7, exp(4), sqrt(54), -0.124)\):
- Find the length of
x
. - Which elements of of
x
are between 0 (included) AND 1 (excluded)? Hint: the AND operator is given by&
. Compute also the corresponding absolute (count) and relative frequency (proportions). - Which elements of
x
are negative? Substitute them with the same number in absolute value. - Extract from
x
the 2nd and 4th value and save them in a new vector namedy
. Compute \(y+sqrt(exp(-0.4))\).
3.4.2 Exercise 2
- Read the help pages of the functions
sample
andseq
.
?sample ?seq
- Run the following lines of code and try to understand what it is going on.
#Attention: we set the seed in order to work with the same data
set.seed(2233)
= sample(seq(0,999), 25, replace=T)
xVec xVec
## [1] 513 773 693 506 706 208 111 713 816 773 465 661 561 883 871 158 498 91 95
## [20] 94 685 564 833 746 425
set.seed(3344)
= sample(seq(0,999, length=100), 25, replace=F)
yVec yVec
## [1] 908.18182 888.00000 999.00000 40.36364 433.90909 938.45455 615.54545
## [8] 898.09091 363.27273 817.36364 736.63636 494.45455 242.18182 948.54545
## [15] 302.72727 181.63636 807.27273 555.00000 353.18182 464.18182 797.18182
## [22] 222.00000 766.90909 988.90909 696.27273
set.seed(33)
= sample(seq(0,999, by=10), 5, replace=F)
zVec zVec
## [1] 410 70 850 590 80
Compute some summary statistics for the three vectors.
Select the values in
yVec
which are bigger than 600.Select the values in
yVec
which are between 600 and 800 and save them in a new vector calledyVec_sel1
. Pick out the values inyVec
which are bigger than 600 or lower than 800 and save them in a new vector calledyVec_sel2
. Which is the length ofyVec_sel1
andyVec_sel2
?Which are the values in
xVec
that correspond to the values inyVec
which are bigger than 600? (By correspond, I mean that they have the same positions).Compute the sum and the difference of the first 5 elements of the 2 vectors. Hint: to index the first 5 elements you can use
1:5
.For
xVec
compute the following formula \(\frac{\sum_{i=1}^n (x_i-\bar x)^2}{n}\), where \(n\) is the vector length and \(\bar x\) is the vector mean. Is the result equal to the one obtained withvar
? Why?For
xVec
compute the following formula \(\frac{\sum_{i=1}^n |x_i-Me|}{n}\), where \(n\) is the vector length and \(Me\) is the vector median.Consider
zVec
. Try to understand how the functionssort
andorder
work when applied tozVec
. Check also their help pages.
sort(zVec)
## [1] 70 80 410 590 850
order(zVec)
## [1] 2 5 1 4 3
order(zVec)] zVec[
## [1] 70 80 410 590 850
3.4.3 Exercise 3
Consider the following random variables:
- \(X_1\sim Normal(\mu=10, \sigma=1)\)
- \(X_2\sim Normal(\mu=10, \sigma=2)\)
- \(X_3\sim Normal(\mu=10, \sigma=0.5)\)
- Simulate 1000 values from \(X_1\) (seed=1), from \(X_2\) (seed=2) and from \(X_3\) (seed=3). Plot the distribution of the 3 sets of simulated data.
- Compute for each set of data the percentage of values between 9 and 11.