“Exercises PSIS”

Section 2:

Exercise 2.1

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 2.1_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

a <- 2.3
verify <- (6*a + 42)/(3^(4.2 - 3.62)); verify
## [1] 29.50556

B.

The option that squares negative 4 and adds 2 to the result is number one, here’s the demonstration:

i <- (-4)^2+2; i
## [1] 18

C.

numeros <- c(25.2, 15, 16.44, 15.3, 18.6)
resultado <- sqrt(mean(numeros)/2)
round(resultado,3)
## [1] 3.009

D.

log_res <- log(0.3)
round(log_res,3)
## [1] -1.204

E.

exp_res <- exp(log_res); exp_res
## [1] 0.3

F.

-0.00000000423546322
## [1] -4.235463e-09

Exercise 2.2

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 2.2_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

res <- ((3^2) * 4^(1/8))
round(res,3)
## [1] 10.703

B.

res_2 <- res / 2.33
round(res_2,3)
## [1] 4.594

C.

obj <- -8.2 * 10^(-13); obj
## [1] -8.2e-13

D.

res_2 * obj
## [1] -3.766673e-12

Exercise 2.3

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 2.3_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

sec <- seq(from = 5, to = -11, by = -0.3); sec
##  [1]   5.0   4.7   4.4   4.1   3.8   3.5   3.2   2.9   2.6   2.3   2.0   1.7
## [13]   1.4   1.1   0.8   0.5   0.2  -0.1  -0.4  -0.7  -1.0  -1.3  -1.6  -1.9
## [25]  -2.2  -2.5  -2.8  -3.1  -3.4  -3.7  -4.0  -4.3  -4.6  -4.9  -5.2  -5.5
## [37]  -5.8  -6.1  -6.4  -6.7  -7.0  -7.3  -7.6  -7.9  -8.2  -8.5  -8.8  -9.1
## [49]  -9.4  -9.7 -10.0 -10.3 -10.6 -10.9

B.

sec <- rev(sec); sec
##  [1] -10.9 -10.6 -10.3 -10.0  -9.7  -9.4  -9.1  -8.8  -8.5  -8.2  -7.9  -7.6
## [13]  -7.3  -7.0  -6.7  -6.4  -6.1  -5.8  -5.5  -5.2  -4.9  -4.6  -4.3  -4.0
## [25]  -3.7  -3.4  -3.1  -2.8  -2.5  -2.2  -1.9  -1.6  -1.3  -1.0  -0.7  -0.4
## [37]  -0.1   0.2   0.5   0.8   1.1   1.4   1.7   2.0   2.3   2.6   2.9   3.2
## [49]   3.5   3.8   4.1   4.4   4.7   5.0

C.

vec <- c(-1, 3, -5, 7, -9)
repetido <- rep(vec, 2)
repetido_2 <- rep(repetido, 10)
result <- sort(repetido_2, decreasing = T); result
##   [1]  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  7  3  3  3  3  3
##  [26]  3  3  3  3  3  3  3  3  3  3  3  3  3  3  3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
##  [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
##  [76] -5 -5 -5 -5 -5 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9

D.

vec_almacenamiento <- c((seq(6, 12)), (rep(5.3, 3)), -3, (seq(102, (length(result)), length.out = 9))); vec_almacenamiento
##  [1]   6.00   7.00   8.00   9.00  10.00  11.00  12.00   5.30   5.30   5.30
## [11]  -3.00 102.00 101.75 101.50 101.25 101.00 100.75 100.50 100.25 100.00

E.

length(vec_almacenamiento)
## [1] 20

Exercise 2.4

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 2.4_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

secu <- c((seq(3, 6, length.out = 5)), (rep(c(2,-5.1,-33), 2)), ((7/32)+2)); secu
##  [1]   3.00000   3.75000   4.50000   5.25000   6.00000   2.00000  -5.10000
##  [8] -33.00000   2.00000  -5.10000 -33.00000   2.21875

B.

objeto_1 <- secu[c(1,length(secu))]; objeto_1
## [1] 3.00000 2.21875

C.

objeto_2 <- secu[-c(1,length(secu))]; objeto_2
##  [1]   3.75   4.50   5.25   6.00   2.00  -5.10 -33.00   2.00  -5.10 -33.00

D.

recontruccion <- c(objeto_1[1], objeto_2, objeto_1[2]); recontruccion
##  [1]   3.00000   3.75000   4.50000   5.25000   6.00000   2.00000  -5.10000
##  [8] -33.00000   2.00000  -5.10000 -33.00000   2.21875

E.

vector_ordenado <- sort(recontruccion); vector_ordenado
##  [1] -33.00000 -33.00000  -5.10000  -5.10000   2.00000   2.00000   2.21875
##  [8]   3.00000   3.75000   4.50000   5.25000   6.00000

F.

vector_invertido <- vector_ordenado[length(vector_ordenado):1]; vector_invertido
##  [1]   6.00000   5.25000   4.50000   3.75000   3.00000   2.21875   2.00000
##  [8]   2.00000  -5.10000  -5.10000 -33.00000 -33.00000
vector_ordenado_decreasing <- sort(vector_ordenado, decreasing = T); vector_ordenado_decreasing
##  [1]   6.00000   5.25000   4.50000   3.75000   3.00000   2.21875   2.00000
##  [8]   2.00000  -5.10000  -5.10000 -33.00000 -33.00000
all(vector_invertido == vector_ordenado_decreasing)
## [1] TRUE

G.

nuevo_vector <- c((rep(objeto_2[3],3)), rep(objeto_2[6],4), objeto_2[length(objeto_2)]); nuevo_vector
## [1]   5.25   5.25   5.25  -5.10  -5.10  -5.10  -5.10 -33.00

H.

nuevo_objeto <- vector_ordenado
nuevo_objeto[c(1, 5:7, length(nuevo_objeto))] <- c(99:95); nuevo_objeto
##  [1]  99.00 -33.00  -5.10  -5.10  98.00  97.00  96.00   3.00   3.75   4.50
## [11]   5.25  95.00

Exercise 2.5

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & , Date: 2024-06-30

Script Name: Exercise 2.5_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

vector_original <- c(2, 0.5, 1, 2, 0.5, 1, 2, 0.5, 1)
vector_transformacion <- c(1/2, 2, 1)
vector_transformado <- vector_original * rep(vector_transformacion, length.out = length(vector_original)); vector_transformado
## [1] 1 1 1 1 1 1 1 1 1

B.

temperaturas_f <- c(45, 77, 20, 19, 101, 120, 212)
temperaturas_c <- (5/9)*(temperaturas_f-32)
tabla_temperaturas <- data.frame(
  Fahrenheit = temperaturas_f,
  Celsius = round(temperaturas_c,2)); kableExtra::kable(tabla_temperaturas)
Fahrenheit Celsius
45 7.22
77 25.00
20 -6.67
19 -7.22
101 38.33
120 48.89
212 100.00

C.

vector1 <- c(2, 4, 6)
vector2 <- c(1, 2)
vector2_repetido <- rep(vector2, each = length(vector1))
resultado <- vector1 * vector2_repetido; resultado
## [1]  2  4  6  4  8 12

D.

resultado[2:5] <- c(-0.1, -100); resultado
## [1]    2.0   -0.1 -100.0   -0.1 -100.0   12.0

Section 3:

Exercise 3.1

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 3.1_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

matriz <- matrix(data = c(4.3, 3.1, 8.2, 8.2, 3.2, 0.9, 1.6, 6.5), 4, 2, 
                 byrow = T); matriz
##      [,1] [,2]
## [1,]  4.3  3.1
## [2,]  8.2  8.2
## [3,]  3.2  0.9
## [4,]  1.6  6.5

B.

matriz_2 <- matriz[-1,];matriz_2
##      [,1] [,2]
## [1,]  8.2  8.2
## [2,]  3.2  0.9
## [3,]  1.6  6.5
dim(matriz_2)
## [1] 3 2

C.

matriz[,2] <- sort(matriz[,2]); matriz
##      [,1] [,2]
## [1,]  4.3  0.9
## [2,]  8.2  3.1
## [3,]  3.2  6.5
## [4,]  1.6  8.2

D.

matriz_3 <- matriz[-4, -1]
resultado_1 <- matrix(matriz_3, ncol = 1); resultado_1
##      [,1]
## [1,]  0.9
## [2,]  3.1
## [3,]  6.5

E.

matriz_4 <- matrix(matriz[3:4,], 2); matriz_4
##      [,1] [,2]
## [1,]  3.2  6.5
## [2,]  1.6  8.2

F.

diagonal <- (-1/2) *diag(matriz_4);diagonal
## [1] -1.6 -4.1
matriz[4, 2] <- diagonal[2]
matriz[1, 2] <- diagonal[2]
matriz[4, 1] <- diagonal[1]
matriz[1, 1] <- diagonal[1]
matriz
##      [,1] [,2]
## [1,] -1.6 -4.1
## [2,]  8.2  3.1
## [3,]  3.2  6.5
## [4,] -1.6 -4.1

Exercise 3.2

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 3.2_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

uno <- matrix(c(1,2,7,2,4,6),3)
dos <- matrix(c(10,30,50,20,40,60),3)
round((2/7)*(uno - dos),2)
##        [,1]   [,2]
## [1,]  -2.57  -5.14
## [2,]  -8.00 -10.29
## [3,] -12.29 -15.43

B.

a <- matrix(c(1,2,7),3); a
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    7
b <- matrix(c(3,4,8),3); b
##      [,1]
## [1,]    3
## [2,]    4
## [3,]    8

i.

a * b
##      [,1]
## [1,]    3
## [2,]    8
## [3,]   56

ii.

t(a) %*% b
##      [,1]
## [1,]   67

iii.

t(b) %*% (a %*% t(a))
##      [,1] [,2] [,3]
## [1,]   67  134  469

iv.

# (a %*% t(a)) %*% t(b)

It is not possible to calculate this multiplication since the term \(A * A^t\) has dimension \(3 x 3\) while \(B^T\) has dimension \(1x3\) so multiplication between these 2 matrices is not possible.

v.

i3 <- diag(3)
round(((b %*% t(b)) + (a %*% t(a)) - (100 * i3))^(-1),3)
##        [,1]   [,2]  [,3]
## [1,] -0.011  0.071 0.032
## [2,]  0.071 -0.013 0.022
## [3,]  0.032  0.022 0.077

C.

i4 <- diag(4)
q <-  matrix(diag(c(2,3,5,-1)), 4); q
##      [,1] [,2] [,3] [,4]
## [1,]    2    0    0    0
## [2,]    0    3    0    0
## [3,]    0    0    5    0
## [4,]    0    0    0   -1
solve(q) * q - i4
##      [,1] [,2] [,3] [,4]
## [1,]    0    0    0    0
## [2,]    0    0    0    0
## [3,]    0    0    0    0
## [4,]    0    0    0    0

Exercise 3.3

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 3.3_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

capas <- array(seq(4.8, 0.1, length.out = 48), dim = c(4, 2, 6)); capas
## , , 1
## 
##      [,1] [,2]
## [1,]  4.8  4.4
## [2,]  4.7  4.3
## [3,]  4.6  4.2
## [4,]  4.5  4.1
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  4.0  3.6
## [2,]  3.9  3.5
## [3,]  3.8  3.4
## [4,]  3.7  3.3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  3.2  2.8
## [2,]  3.1  2.7
## [3,]  3.0  2.6
## [4,]  2.9  2.5
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.4  2.0
## [2,]  2.3  1.9
## [3,]  2.2  1.8
## [4,]  2.1  1.7
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  1.6  1.2
## [2,]  1.5  1.1
## [3,]  1.4  1.0
## [4,]  1.3  0.9
## 
## , , 6
## 
##      [,1] [,2]
## [1,]  0.8  0.4
## [2,]  0.7  0.3
## [3,]  0.6  0.2
## [4,]  0.5  0.1

B.

nuevas_capas <- capas[c(4,1),2, ]; nuevas_capas
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  4.1  3.3  2.5  1.7  0.9  0.1
## [2,]  4.4  3.6  2.8  2.0  1.2  0.4

C.

array(rep(nuevas_capas[2, ],4), c(2, 2, 2, 3))
## , , 1, 1
## 
##      [,1] [,2]
## [1,]  4.4  2.8
## [2,]  3.6  2.0
## 
## , , 2, 1
## 
##      [,1] [,2]
## [1,]  1.2  4.4
## [2,]  0.4  3.6
## 
## , , 1, 2
## 
##      [,1] [,2]
## [1,]  2.8  1.2
## [2,]  2.0  0.4
## 
## , , 2, 2
## 
##      [,1] [,2]
## [1,]  4.4  2.8
## [2,]  3.6  2.0
## 
## , , 1, 3
## 
##      [,1] [,2]
## [1,]  1.2  4.4
## [2,]  0.4  3.6
## 
## , , 2, 3
## 
##      [,1] [,2]
## [1,]  2.8  1.2
## [2,]  2.0  0.4

D.

nueva_capa_2 <- capas[ , , -6];nueva_capa_2
## , , 1
## 
##      [,1] [,2]
## [1,]  4.8  4.4
## [2,]  4.7  4.3
## [3,]  4.6  4.2
## [4,]  4.5  4.1
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  4.0  3.6
## [2,]  3.9  3.5
## [3,]  3.8  3.4
## [4,]  3.7  3.3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  3.2  2.8
## [2,]  3.1  2.7
## [3,]  3.0  2.6
## [4,]  2.9  2.5
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.4  2.0
## [2,]  2.3  1.9
## [3,]  2.2  1.8
## [4,]  2.1  1.7
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  1.6  1.2
## [2,]  1.5  1.1
## [3,]  1.4  1.0
## [4,]  1.3  0.9

E.

nueva_capa_2[c(2, 4), 2, c(1, 3 ,5)] <- 99;nueva_capa_2
## , , 1
## 
##      [,1] [,2]
## [1,]  4.8  4.4
## [2,]  4.7 99.0
## [3,]  4.6  4.2
## [4,]  4.5 99.0
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  4.0  3.6
## [2,]  3.9  3.5
## [3,]  3.8  3.4
## [4,]  3.7  3.3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  3.2  2.8
## [2,]  3.1 99.0
## [3,]  3.0  2.6
## [4,]  2.9 99.0
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.4  2.0
## [2,]  2.3  1.9
## [3,]  2.2  1.8
## [4,]  2.1  1.7
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  1.6  1.2
## [2,]  1.5 99.0
## [3,]  1.4  1.0
## [4,]  1.3 99.0

Section 4

Exercise 4.2

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 4.2_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

foo <- c(7,1,7,10,5,9,10,3,10,8)
resultado1 <- foo[foo > 5 | foo == 2]

B.

bar <- c(8,8,4,4,5,1,5,6,6,8)
resultado2 <- bar[bar <= 6 & bar != 4]

C.

global <- c(resultado1,resultado2); global
##  [1]  7  7 10  9 10 10  8  5  1  5  6  6

D.

baz <- foo + bar;baz
##  [1] 15  9 11 14 10 10 15  9 16 16

i.

baz[baz >= 14 & baz != 15]
## [1] 14 16 16

ii.

division <- baz / foo
division[division > 4 | division <= 2]
## [1] 9.000000 1.571429 1.400000 2.000000 1.111111 1.500000 1.600000 2.000000

Note: Exercise E cannot be completed because the long version has not been learned.

Exercise 4.3

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 4.3_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A.

foo <- c(7,5,6,1,2,10,8,3,8,2)

i.

bar <- foo[foo >= 5]

ii.

foo[foo < 5]
## [1] 1 2 3 2

B.

baz <- matrix(bar, 2, 3, byrow = T);baz
##      [,1] [,2] [,3]
## [1,]    7    5    6
## [2,]   10    8    8

i.

baz[baz == 8] <- (baz[1,2])^2;baz
##      [,1] [,2] [,3]
## [1,]    7    5    6
## [2,]   10   25   25

ii.

baz <= 25 & baz > 4
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE

C.

qux <- array(c(10,5,1,4,7,4,3,3,1,3,4,3,1,7,8,3,7,3), c(3,2,3));qux
## , , 1
## 
##      [,1] [,2]
## [1,]   10    4
## [2,]    5    7
## [3,]    1    4
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    3    3
## [2,]    3    4
## [3,]    1    3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    1    3
## [2,]    7    7
## [3,]    8    3

i.

indices <- which(qux == 3 | qux == 4, arr.ind = T, useNames = F);indices
##       [,1] [,2] [,3]
##  [1,]    1    2    1
##  [2,]    3    2    1
##  [3,]    1    1    2
##  [4,]    2    1    2
##  [5,]    1    2    2
##  [6,]    2    2    2
##  [7,]    3    2    2
##  [8,]    1    2    3
##  [9,]    3    2    3
qux == 3 | qux == 4
## , , 1
## 
##       [,1]  [,2]
## [1,] FALSE  TRUE
## [2,] FALSE FALSE
## [3,] FALSE  TRUE
## 
## , , 2
## 
##       [,1] [,2]
## [1,]  TRUE TRUE
## [2,]  TRUE TRUE
## [3,] FALSE TRUE
## 
## , , 3
## 
##       [,1]  [,2]
## [1,] FALSE  TRUE
## [2,] FALSE FALSE
## [3,] FALSE  TRUE

Note: Exercise D cannot be completed because exercises 4.1 were not done

Exercise 4.4

Statistical programming on the R plataform

Faculty of Economics - Course: MINE_008

Author: Nicolás Romero Alfonso & Laura Molina.

Identity card: 1000573650 & 1000273009

Copyright 2024

Email: , & ,

Date: 2024-06-30

Script Name: Exercise 4.4_R_Script_MINE_VIII_Nicolás-Romero_Laura-Molina

Description: Proper syntax exercises to strengthen programming skills in R

Version: 1.0

First steps

rm(list = ls())                                    
objects()
## character(0)
getwd()  
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts/prog_001"
basename(getwd())
## [1] "prog_001"
dirname(getwd())
## [1] "C:/Users/nicor/OneDrive - Universidad Externado de Colombia/MINE - 008/PSIS/Scripts"
cat("/014")     
## /014

A

cat("\"The quick brown fox\n  jumped over\n    the lazy dogs\"")
## "The quick brown fox
##   jumped over
##     the lazy dogs"

B

num1 <- 4
num2 <- 0.75
resultado <- num1 * num2; cat("The result of multiplying", num1, "by", num2, "is", resultado)
## The result of multiplying 4 by 0.75 is 3

C

directorio <- "G:/Users/Laura/OneDrive - Universidad Externado de Colombia/Documents/prog_001"
nombre <- "LMolina" 
nuevo_directorio <- sub("prog_001", nombre, directorio); nuevo_directorio
## [1] "G:/Users/Laura/OneDrive - Universidad Externado de Colombia/Documents/LMolina"

D

bar <- "How much wood could a woodchuck chuck"

i

nuevo_bar <- paste(bar, "if a woodchuck could chuck wood"); nuevo_bar
## [1] "How much wood could a woodchuck chuck if a woodchuck could chuck wood"

ii

nuevo_bar_2 <- gsub("wood", "metal", nuevo_bar); nuevo_bar_2
## [1] "How much metal could a metalchuck chuck if a metalchuck could chuck metal"

E

string <- "Two 6-packs for $12.99"

i

string_2 <- substr(string, 5, 10); string_2 == "6-pack"
## [1] TRUE

iii

mejor_trato <- sub("\\$12.99", "$10.99", string); mejor_trato
## [1] "Two 6-packs for $10.99"