3 Lecture 1 - 07/12/2020
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:
## [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 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
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:
## [1] 0.69897
## [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).
## [1] 0.69897
## [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!):
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
:
To visualize the values of y
just run the object name (remember that R is case-sensitive so that y
is different from Y
):
## [1] -1.50000 99.00000 2.70805 6.70000
The vector length is given by
## [1] 4
It is possible to compute operations with vector. For example, the following code
## [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:
## [1] 6.890000 -10.000000 5.500000 1.268636
## [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:
## [1] 99
To select more than one element, a vector of positions is provided:
## [1] -1.5 6.7
If you need to select the first four element of y
you can proceed as follows:
## [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] -1.50000 99.00000 2.70805 6.70000
To replace values (after having selected them) we use the assignment operator =
:
## [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:
## [1] 11.90805
## [1] 2.977013
## [1] 2.977013
## [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:
## [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.
## [1] 3
## [1] 0.75
## [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:
## [1] 0 -10 0 0
3.3 Exercises Lecture 1
3.3.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 a number simulated from the standard Normal distribution (in this case we don’t care about the seed). - Extract from
x
the 2nd and 4th value and save them in a new vector namedy
. Compute \(y+sqrt(exp(-0.4))\).
3.3.2 Exercise 2
Simulate \(n=500\) values from the standard Normal distribution. Save the values in a vector named x
. Use the value 678 as seed.
Represent graphically
x
(use the functionhist
).Compute the min, the max and the mean of
x
. Check these numbers in the plot obtained before.How many values (total number and percentage) of
x
are between -1 and +1 (extremes excluded)? How many are lower than 0?Compute \(x/4-2\) and assign the result to a new object called
y
.[Theoretical exercise] Do you think that
y
can be considered as randomly generated from another Normal distribution? Which one (which parameters)?Check that the empirical mean and variance are close the expected theoretical values defined previously.
3.3.3 Exercise 3
- Read the help pages of the functions
sample
andseq
.
- 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)
xVec = sample(seq(0,999), 25, replace=T)
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
## [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
## [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. Use Google to discover how to compute the absolute value withR
.Consider
zVec
. Try to understand how the functionssort
andorder
work when applied tozVec
. Check also their help pages.
## [1] 70 80 410 590 850
## [1] 2 5 1 4 3
## [1] 70 80 410 590 850