N.B.: before starting any exercise that involves random numbers, set the seed to 1303 (i.e., execute set.seed(1303))
# 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
# Class solution:
n = 4
v<-seq(5,5+n)
v
# Solution
n <-6
seq(5,5+n)
# Alternative
n <-6
ls2 <- list(5:(n+5))
print(ls2)
ls3 <- c(5:(n+5))
print(ls3)
# 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)
# Class solution:
a = 2
b = 5
#(a+1):(b-1)
sum(seq(a,b))
# Solution
a <- 5
b <- 10
sum(a:b)
#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)
# Class solution:
n <- 5
a <- 1
f <- seq(1:n)
for(i in f){
a <- a*i
}
print(a)
# 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
#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
x <- c(4,5,8,10,3,5,13)
x < 7
#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])
for(i in x){
if(i<7)
print(i)
}
# Solution 1
for(i in x){
if(i<7)
print(i)
}
# Solution 2
#test <- x<7
#x[test]
# ... or directly...
x[x<7]
?seq
#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)
a <- seq(1,10)
ls <-seq(2,10,2)
a[ls] = ls-1
a[ls-1] = ls
print(a)
# and the winner is...
ls <-seq(2,10)
ls
# 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
# 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
# Ex5 (bis): exercise 3 using an array instead of a list
# and the while instead of repeat
# Example using arrays
# Solution
# Step 1: create an array with all even number (equal to 2)
v <- array(2,dim=c(1,10))
n <- length(v)
# Step 2: change all elements with even index to an odd number (equal to 1)
cnt = 0
while (cnt < n){
if(cnt %% 2)
v[cnt] = 1
cnt = cnt+1
}
v
# Ex 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.
set.seed(1303)
A = sample(1:10, 5, replace=TRUE)
B = c()
C = c()
for (element in A){
if (element%%2 == 0)
B = c(B,element)
else
C = c(C,element)
}
print(A)
print(B)
print(C)
help(sample)
set.seed(1303)
el_vector = sample(1:100, 10)
odd = c()
even = c()
for (el in el_vector){
# if (el ==0)
# next
if(el%%2==0)
even = c(even,el)
else
odd = c(odd,el)
}
print(el_vector)
print(odd)
print(even)
#Solution
set.seed(1303)
# take 8 samples of numbers between 1 and 100
A <- sample(1:100, size= 8)
A
# Create two empty lists B and C
B <-c()
C <-c()
for (el in A)
{
if(el %% 2 == 0)
B <- c(B,el) # insert a single element in B
else
C <- c(C,el) # insert a single element in C
}
B
C
# Ex 7:
#Create a list A with all random numbers.
#Then, create a list B containing all the elements of A multiplied by 4.
A <- sample(1:100, 5)
A
B <- A * 4
B
# the proof...
B / 4
# Alternative
A <- sample(15,5)
B <- c()
for(n in A){
B <- c(B, n*4)
}
A
B
# Ex.8: Create a 10x10 matrix with all random numbers from
# a normal distribution.
set.seed(1303)
A = matrix(rnorm(100), nrow=10)
A
A <- matrix(rnorm(100), 10,10)
A
matr <- matrix(rnorm(100), nrow=10, ncol=10)
#print(matr)
# the proof..
hist(matr)
mean(matr)
sd(matr)
# Ex 9: Create a 10x10 matrix which general
# element M[i][j] has value equal to (i+1)*(j+1).
# Note: by passing 'NA' as data, an empty matrix is created
M = matrix(1:100, nrow=10)
for(i in 1:10)
for(j in 1:10)
M[i,j] = (i+1)*(j+1)
M
A = matrix('NA', 10, 10)
for(i in 1:nrow(A)){
for(j in 1:ncol(A)){
A[i,j] = (i+1)*(j+1)
}
}
A
matr <- matrix(NA, ncol=10, nrow=10)
for(i in 1:10){
for(j in 1:10){
matr[i, j] <- (i+1)*(j+1)
}
}
print(matr)
# M[i][j] has value equal to (i+1)*(j+1)
# Ex 10: Create a 10x10 matrix with all random numbers from
# a normal distribution. Then, sum all the elements of
# the principal diagonal.
set.seed(1303)
M = matrix(rnorm(100), nrow=10)
sum = 0
j=1
for(i in 1:nrow(M)){
sum = sum + M[i,j]
print(M[i,j])
j = j+1
}
M
print(sum)
s = 0
A = matrix(rnorm(100),10,10)
for(i in 1:nrow(A))
s = s + A[i,i]
A
print(s)
n <- 10
matr <- matrix(rnorm(n*n), nrow=n, ncol=n)
i <- 1
s <- 0
while(i<n+1){
s <- s + matr[i,i]
i <- i + 1
}
print(matr)
print(s)
# Alternative
matr <- matrix(rnorm(9), nrow=3, ncol=3)
s <- 0
for(i in 1:3){
s <- s + matr[i,i]
}
print(matr)
print(s)
set.seed(1303)
M = matrix(rnorm(100),nrow=10)
S = sum(diag(M))
print(S)
?diag
M = matrix(c(1,2,3,4,5,6,7,8,9), nrow=3)
M
diag(M)
# Ex 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.
set.seed(1303)
data = rnorm(1000,mean=50,sd=3)
hist(data)
hist(rnorm(1000, 50, 3))
#?rnorm
data <- rnorm(1000, mean = 50, sd = 3)
hist(data)
set.seed(1303)
S = rnorm(1000,mean=50,sd=3)
hist(S,col="red")
xfit = seq(min(S),max(S),length=40)
yfit = dnorm(xfit,mean=mean(S),sd=sd(S))
yfit = yfit*diff(S[1:2])*length(S)*0.25
lines(xfit,yfit,col="blue",lwd=2)
set.seed(1303)
S = rnorm(1000,mean=50,sd=3)
hist(S,prob=TRUE)
lines(density(S))
# ... and the winner is... ???
X = rnorm(1000,mean=50,sd=3)
#hist(X,prob=TRUE)
x2 = seq(min(X),max(X),length=40)
fun= dnorm(x2,mean=mean(X),sd = sd(X))
hist(X,prob=TRUE,
col="white",
ylim = c(0,max(fun)),
main = "Histogram")
lines(x2,fun,col=2,lwd=2)
?hist
# Ex 12: Write a while loop that
# prints out 10 standard random normal numbers.
# Note: in this case the numbers belong to separate distr.
cnt <- 10 #(cnt from 10 to 0)
while(cnt > 0){
n <- rnorm(1)
print(n)
cnt <- cnt - 1
}
# Alternative (cnt from 0 to 10)
cnt <- 0
while(cnt<10){
n <- rnorm(1)
print(n)
cnt <- cnt +1
}
# Note: in this case the numbers belong to the same distr.
data <- rnorm(10)
for (n in data){
print(n)
}
# Ex 13: Write a for loop that iterates over the
# numbers 1 to 7 and prints the cube of each number
# using print().
for(n in 1:7){
n <- n^3
print(n)
}
# Ex 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.
Age <- c(22, 25, 18, 20)
Name <- c("Marco", "Fabio", "Sara", "Francesca")
Gender <- c("M", "M", "F", "F")
data.frame(Age, Name, Gender)