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:

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 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!):

x = -1.5

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:

y = c(-1.5, 99, log(15), 6.7)

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

y + 4 
## [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
z = c(6.89, -10, 5.5, sqrt(log(5))) 
z 
## [1]   6.890000 -10.000000   5.500000   1.268636
# operations with 2 vectors
y - z
## [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:

y[2] 
## [1] 99

To select more than one element, a vector of positions is provided:

y[c(1,4)] #select the first and forth element
## [1] -1.5  6.7

If you need to select the first four element of y you can proceed as follows:

y[c(1,2,3,4)]
## [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:

y[1:4]
## [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
y[2] = 4
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:

z >= 0
## [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
z[z > 0] = 0
z
## [1]   0 -10   0   0

3.3 Exercises Lecture 1

3.3.1 Exercise 1

  1. Compute \(\exp(3-\frac{4}{5})+\frac{\sqrt{3+2^5}}{4-7\cdot \log(10))}\)

  2. 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 named y. 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.

  1. Represent graphically x (use the function hist).

  2. Compute the min, the max and the mean of x. Check these numbers in the plot obtained before.

  3. How many values (total number and percentage) of x are between -1 and +1 (extremes excluded)? How many are lower than 0?

  4. Compute \(x/4-2\) and assign the result to a new object called y.

  5. [Theoretical exercise] Do you think that y can be considered as randomly generated from another Normal distribution? Which one (which parameters)?

  6. Check that the empirical mean and variance are close the expected theoretical values defined previously.

3.3.3 Exercise 3

  1. Read the help pages of the functions sample and seq.
?sample
?seq
  1. 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
set.seed(3344)
yVec = sample(seq(0,999, length=100), 25, replace=F)
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)
zVec = sample(seq(0,999, by=10), 5, replace=F)
zVec
## [1] 410  70 850 590  80
  1. Compute some summary statistics for the three vectors.

  2. Select the values in yVec which are bigger than 600.

  3. Select the values in yVec which are between 600 and 800 and save them in a new vector called yVec_sel1. Pick out the values in yVec which are bigger than 600 or lower than 800 and save them in a new vector called yVec_sel2. Which is the length of yVec_sel1 and yVec_sel2?

  4. Which are the values in xVec that correspond to the values in yVec which are bigger than 600? (By correspond, I mean that they have the same positions).

  5. 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.

  6. 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 with var? Why?

  7. 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 with R.

  8. Consider zVec. Try to understand how the functions sort and order work when applied to zVec. Check also their help pages.

sort(zVec) 
## [1]  70  80 410 590 850
order(zVec) 
## [1] 2 5 1 4 3
zVec[order(zVec)]
## [1]  70  80 410 590 850