Class Exercises

Statistical Laboratory

Alessandro Ortis - University of Catania

Exercises:

N.B.: before starting any exercise that involves random numbers, set the seed to 1303 (i.e., execute set.seed(1303))

  1. Given a number n, generate a list containing the first n integer numbers starting from 5.
  2. Given two numbers a and b, compute the sum of the numbers between a and b.
  3. Given a number n, compute the factorial of n. The factorial of a number n can be defined as product of all positive numbers less than or equal to n.
  4. Consider a vector x <- c(4,5,8,10,3,5,13), what is the value of x<7?
  5. Create a list with all odd numbers in the even positions and vice-versa.
  6. Create a list A with all random numbers. Then, create a list B containing only the odd elements of A and a list C containing only the even elements of A.
  7. Create a list A with all random numbers. Then, create a list B containing all the elements of A multiplied by 4.
  8. Create a 10x10 matrix with all random numbers from a normal distribution.
  9. Create a 10x10 matrix which general element M[i][j] has value equal to (i+1)*(j+1).
  10. Create a 10x10 matrix with all random numbers from a normal distribution. Then, sum all the element of the principal diagonal.
  11. Generate 1000 random numbers of a Gaussian with mean = 50 and std dev. = 3. Then draw a plot to show the distribution of the data.
  12. Write a while loop that prints out 10 standard random normal numbers.
  13. Write a for loop that iterates over the numbers 1 to 7 and prints the cube of each number using print().
  14. Create a vector 'age' with four integer example values of age, create a vector 'name' of four strings with four name examples, create a vector 'gender' with four elements with values equal to 'M' or 'F'. Then, create a dataframe which rows are four records and columns are 'age', 'name' and 'gender' contained in the previous defined vectors.
In [3]:
# Ex 1:
#Given a number n, generate a list containing the first 
# n integer numbers starting from 5.

#Class solution:
n = 30
lista = seq(5,n+5-1)
lista
  1. 5
  2. 6
  3. 7
  4. 8
  5. 9
  6. 10
  7. 11
  8. 12
  9. 13
  10. 14
  11. 15
  12. 16
  13. 17
  14. 18
  15. 19
  16. 20
  17. 21
  18. 22
  19. 23
  20. 24
  21. 25
  22. 26
  23. 27
  24. 28
  25. 29
  26. 30
  27. 31
  28. 32
  29. 33
  30. 34
In [1]:
# Class solution:
n = 4
v<-seq(5,5+n)
v
  1. 5
  2. 6
  3. 7
  4. 8
  5. 9
In [6]:
# Solution
n <-6
seq(5,5+n)

# Alternative
n <-6
ls2 <- list(5:(n+5))
print(ls2)

ls3 <- c(5:(n+5))
print(ls3)
  1. 5
  2. 6
  3. 7
  4. 8
  5. 9
  6. 10
  7. 11
[[1]]
[1]  5  6  7  8  9 10 11

[1]  5  6  7  8  9 10 11
In [4]:
# Ex 2: Given two numbers a and b, 
# compute the sum of the numbers between a and b.

a = 3
b = 7

sum = 0
for(e in a:b){
    sum = sum + e
}
print(sum)
[1] 25
In [6]:
# Class solution:
a = 2
b = 5
#(a+1):(b-1)
sum(seq(a,b))
14
In [9]:
# Solution
a <- 5
b <- 10
sum(a:b)
45
In [5]:
#Ex 3: 
#Given a number n, compute the factorial of n. 
#The factorial of a number n can be defined as product 
# of all positive numbers less than or equal to n.

n = 3
fac = 1

while(n>0){
    fac = fac * n
    n = n -1
}
print(fac)
[1] 6
In [8]:
# Class solution:
n <- 5
a <- 1
f <- seq(1:n)
for(i in f){
    a <- a*i
}
print(a)
[1] 120
In [9]:
# Solution
n <- 5
f <- 1
while(n>1){
    f <- f * n
    n <- n - 1
}
f

# Alternative
n = 5
f = n
i = 0
while(i < n-1){
    i = i+1
    f = f*i
}
f

# Alternative
f = 1
for(i in (1:n)){
    f = f * i
}
f
120
120
120
In [6]:
#Ex 4:
# Consider a vector x <- c(4,5,8,10,3,5,13), what is the value of x < 7 ?
x <- c(4,5,8,10,3,5,13)
x < 7
  1. TRUE
  2. TRUE
  3. FALSE
  4. FALSE
  5. TRUE
  6. TRUE
  7. FALSE
In [10]:
x <- c(4,5,8,10,3,5,13)
x < 7
  1. TRUE
  2. TRUE
  3. FALSE
  4. FALSE
  5. TRUE
  6. TRUE
  7. FALSE
In [9]:
#Ex 4 (bis):
# print the values of x that are lower than 7
x <- c(4,5,8,10,3,5,13)
for(el in x){
    if(el<7){
        print(el)
    }
}

#Alternative solution
print(x[x<7])
[1] 4
[1] 5
[1] 3
[1] 5
[1] 4 5 3 5
In [17]:
for(i in x){
    if(i<7)
        print(i)
}
[1] 4
[1] 5
[1] 3
[1] 5
In [19]:
# Solution 1
for(i in x){
    if(i<7)
        print(i)
}




# Solution 2
#test <- x<7
#x[test]

# ... or directly...
x[x<7]
[1] 4
[1] 5
[1] 3
[1] 5
  1. 4
  2. 5
  3. 3
  4. 5
In [1]:
?seq
In [15]:
#Ex 5: 
#(define a code that is able to...) Create a list with all odd numbers in the even positions
# and vice-versa.

input <- x
e <- input[input %% 2 == 0] # TRUE in the even positions
o <- input[input %% 2 != 0] # TRUE in the odd positions

l <- c()
i <- 1
j <- 1
while(i<=length(input)){
    l = append(l,e[j])
    l = append(l,o[j])
    i = i + 2 
    j = j + 1
}
print(l)
[1]  5  4  3  8  5 10 13 NA
In [16]:
a <- seq(1,10)
ls <-seq(2,10,2)
a[ls] = ls-1
a[ls-1] = ls
print(a)
 [1]  2  1  4  3  6  5  8  7 10  9
In [9]:
# and the winner is...
ls <-seq(2,10)
ls
  1. 2
  2. 3
  3. 4
  4. 5
  5. 6
  6. 7
  7. 8
  8. 9
  9. 10
In [23]:
# Solution

ls <- list(2,2,2,2,2,2,2)
n <- length(ls)

cnt <- 2
repeat{
    ls[cnt] <- 1
    cnt = cnt + 2
    if(cnt>=n)
        break
}
ls
  1. 2
  2. 1
  3. 2
  4. 1
  5. 2
  6. 1
  7. 2
In [2]:
# More challenging way...

x <- seq(1:9)
j <- 1

i = 1
n = length(x)
while(i < n+1){
    x[i] = x[i] +1
    i = i +1
}

x
  1. 2
  2. 3
  3. 4
  4. 5
  5. 6
  6. 7
  7. 8
  8. 9
  9. 10