Aprenda R Resolviendo

MarĂ­a Fernanda Abril Palacios

David Felipe GarcĂ­a Ayala

Exercises 1

#1-4
3*4+6
## [1] 18
cos(5*pi/180)
## [1] 0.9961947
x<-4
3*sqrt(6+x)
## [1] 9.486833
y<-5.2

#5.
x <- 3
y <- 4
z <- (2*x) - (7*y)
z
## [1] -22
#6.
help(inv)
## No documentation for 'inv' in specified packages and libraries:
## you could try '??inv'
#7-9
A<-matrix(data=c(1,0,1,-2,0,5,2,0,2,2,3,1,-3,2,4,3),nrow=4,ncol=4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    2   -3
## [2,]    0    5    2    2
## [3,]    1    2    3    4
## [4,]   -2    0    1    3
b<-c(1,2,3,4)
C<-A%*%b
C
##      [,1]
## [1,]   -5
## [2,]   24
## [3,]   30
## [4,]   13
#10.
X<-matrix(data=c(5,1,2,3),nrow =2,ncol = 2)
X
##      [,1] [,2]
## [1,]    5    2
## [2,]    1    3
Y<-c(3,-1)
solve(X,Y)
## [1]  0.8461538 -0.6153846
#Para resolver por el metodo de eliminacion gaussiana primero definimos los dos vectores
a<-c(5,2,3)
b<-c(1,3,-1)

#Multiplicamos el vector 2 por -5 y lo sumamos al vector 1 para eliminar x1
b<-b*-5
b
## [1]  -5 -15   5
c<-a+b
c
## [1]   0 -13   8
#Ahora podemos calcular el valor de x2
x2<-8/-13
x2
## [1] -0.6153846
#Finalmente, con el valor de x2 podemos hallar x1
x1<--1-3*x2
x1
## [1] 0.8461538
#11.
#Resolver el sistema de ecuaciones usando matriz inversa 
X<-matrix(data=c(5,1,2,3),nrow =2,ncol = 2)
X
##      [,1] [,2]
## [1,]    5    2
## [2,]    1    3
Y<-c(3,-1)

#Calculamos la matriz inversa de x

I<-solve(X)
#Finalmente, se debe multiplicar la matriz inversa por el vector Y para calcular los valores de x1 y x2

I%*%Y
##            [,1]
## [1,]  0.8461538
## [2,] -0.6153846
#12-16
M <- matrix( data=c(1,1,4,0,2,5,6,3,-2), nrow = 3, ncol= 3)
subM <- M[2:3,1:2]
M[1,]
## [1] 1 0 6
M[1,3]
## [1] 6
#17-18
v <- c(1:9)
yv <- v^3-2
plot(v,yv)

#19
ms <- matrix(data = c(5,69,4,20,6,18,7,67,19,3,70,6,68,8,17,5), nrow=4, ncol=4)
suma_row = c()
suma_col = c()
diag1 = 0
diag2 = 0
for (i in 1:4){
  suma_col[i]= sum(ms[,i])
  suma_row[i]= sum(ms[i,])
  diag1 = diag1 + ms[i,i]
  diag2 = diag2 + ms[i,5-i]
}
suma_col
## [1] 98 98 98 98
suma_row
## [1] 98 98 98 98
diag1
## [1] 98
diag2
## [1] 98

Exercises 2

#1.
  w<-2*3+7

#2.
a<-4
b<--10
c<-3.2

#3.
y<-10
Y<-100
#Las variables son numericas. Sin embargo, son diferentes debido a que una esta en mayuscula y la otra en minuscula

#4.
X<-5.5
Y<--2.6
Z<-(2*X)-(3*Y)
Z
## [1] 18.8
#5.
W<-(3*Y)-Z+(X/Y)
W
## [1] -28.71538
#6.
r<-6.3
s<-5.8
final<-r+s-(r*s)
final
## [1] -24.44
#7.
this_is_the_result<-(r^2)-(s^2)
this_is_the_result
## [1] 6.05
#8
width<-1.5
Width<-2
WIDTH<-4.5
#Las variables son numericas. Sin embargo, son diferentes debido al uso de las mayusculas

#9.
#This line will no be executed

#10.
s<-3.5 #la variable s toma el valor de 3.5

#11.
y1<-7
y2<-9
y3<-y1-(y2/3)

#12.
#2*m-5     
#se presenta un error debido a que la variable m no esta definida

#13.
cost<-175
profit<-25
sale_price=cost+profit

#14.
centigrade<-28
fahrenheit<-(centigrade*9/5)+32

#15.
j<-14/9
format(j, digits =5) #4 decimales
## [1] "1.5556"
format(j, digits =16) #16 digitos
## [1] "1.555555555555556"
#19.
l<-5
b<-7

area<-b*l
perimetro<-l+b 

#20.
r<-6.45
pi<-3.1416
area<-pi*r^2
perimetro<-2*pi*r

#21
x<-4/5
z<-14/17

#22
y<-2*x-z
  
#23
r<-2/3
area<-pi*r^2

#24
r<-2/3
volumen<-(4*pi/3)*r^3

#25
area1<-as.double(area)
area1
## [1] 1.396267
#26
volumen1<-as.double(volumen)
volumen1
## [1] 1.241126

Exercises 3

#1-14
sqrt(10)
## [1] 3.162278
factorial(7)
## [1] 5040
cos(45*180/pi)
## [1] -0.586942
cos(45)
## [1] 0.525322
sin(45)
## [1] 0.8509035
tan(45)
## [1] 1.619775
atan(1.5)
## [1] 0.9827937
tan(3*pi/2)
## [1] -90747.27
exp(3)
## [1] 20.08554
log(3.5)
## [1] 1.252763
log10(3.5)
## [1] 0.544068
round(2.43)
## [1] 2
5%%4
## [1] 1
abs(-3.6)
## [1] 3.6
#15-17
1.5-2*sqrt(6.7/5)
## [1] -0.8151674
sin(180)^2+ cos(180)^2
## [1] 1
log10(0)
## [1] -Inf
#18
l <- 3*180/2
k <- 2*180
2*sin(l)*cos(k)
## [1] 0.09988533
h <- 45
format(sqrt(45), digits = 2)
## [1] "6.7"
###Calcular simbolicamente??  

Exercises 4

#1.
w<-c(2,4,-6,0)

#2.
w[2]
## [1] 4
#3-8
z<-(pi/2)*w
z
## [1]  3.1416  6.2832 -9.4248  0.0000
z[4]
## [1] 0
z[1:3]
## [1]  3.1416  6.2832 -9.4248
length(z)
## [1] 4
sum(z)
## [1] 4.440892e-16
min(z)
## [1] -9.4248
max(z)
## [1] 6.2832
#9.
r<-seq(1,10,2.5)

#10.
s<-seq(1,100,10)

#11.
v1<-c(9,3,-2,5,0)
v2<-c(1,2,-4)
unir<-c(v1,v2)

#12.
v3<-c(v1,4)

#13.
v1<-c(0.2,1.3,-3.5)
v2<-c(0.5,-2.5,1.0)

#14.
resta<-v1-v2

#15.
multiplicacion<-v1%*%v2

#16.
multiplicacion<-v1*v2

#17.
division<-v1/v2

#18
producto_punto = 0
for (i in 1:3){
  producto_punto = producto_punto + v1[i]*v2[i]
}
producto_punto
## [1] -6.65
#19-20
#Error : La longitud del objeto de mayor longitud no es multiplo de la longitud del objeto de menor longitud 
#s <- c(1, 3, 5) + c(3, 6)
#s <- c(1, 3, 5) - c(3, 6)

#21-25
w <- c(1.1, 1.3, -2.4)
5+w
## [1] 6.1 6.3 2.6
-2-w
## [1] -3.1 -3.3  0.4
1.5*w
## [1]  1.65  1.95 -3.60
w/10
## [1]  0.11  0.13 -0.24
3-2*w/5
## [1] 2.56 2.48 3.96
#26
b <- c(0, pi/3, 2*pi/3, pi)
sin(b)
## [1]  0.000000e+00  8.660266e-01  8.660230e-01 -7.346410e-06
cos(b)
## [1]  1.0000000  0.4999979 -0.5000042 -1.0000000
tan(b)
## [1]  0.000000e+00  1.732061e+00 -1.732031e+00  7.346410e-06
#27-28
exp(b)
## [1]  1.000000  2.849661  8.120567 23.140863
sqrt(b)
## [1] 0.000000 1.023328 1.447204 1.772456
#29
3^b
## [1]  1.000000  3.159667  9.983498 31.544535
#31-32
c(1:4)
## [1] 1 2 3 4
c(1:6)
## [1] 1 2 3 4 5 6
#33
sort(c(0.35, -1.0, 0.24, 1.30, -0.03))
## [1] -1.00 -0.03  0.24  0.35  1.30
#34
sample(1:5, 5, replace = FALSE)
## [1] 4 3 1 2 5
#35
z <- c(2,4,-3,-0,-1,-5,-7)
range(z)
## [1] -7  4
mean(z)
## [1] -1.428571
median(z)
## [1] -1
#36-39
x <- c("r","s","t","u","v")
z <- as.character(c(1,0,-2,3,5))
y <- c()
for (i in 1:5){
  y[i] <- paste(x[i],z[i],sep="+")
}
y
## [1] "r+1"  "s+0"  "t+-2" "u+3"  "v+5"
y[3] <- "t-2"
w <- c()
for (i in 1:5){
  w[i] <- paste("2*",x[i],"/7+3*", y[i], sep = "")
}
w
## [1] "2*r/7+3*r+1" "2*s/7+3*s+0" "2*t/7+3*t-2" "2*u/7+3*u+3" "2*v/7+3*v+5"
#40.
producto_2 <- c()
for (i in 1:5){
  producto_2[i] = paste(x[i],"*",y[i])
}
producto_2
## [1] "r * r+1" "s * s+0" "t * t-2" "u * u+3" "v * v+5"
producto_2[1] <- "r^2+r"
producto_2[2] <- "s^2"
producto_2[3] <- "t^2-2t"
producto_2[4] <- "u^2+3u"
producto_2[5] <- "v^2+5v"
paste(producto_2[1],producto_2[2], producto_2[3], producto_2[4], producto_2[5], sep = "+")
## [1] "r^2+r+s^2+t^2-2t+u^2+3u+v^2+5v"
#41.
q<-c()
for (i in 1:5){
  q[i] <- paste(x[i],y[i],sep="+")
}
q
## [1] "r+r+1" "s+s+0" "t+t-2" "u+u+3" "v+v+5"

Exercises 5

#1.
A<-matrix(data=c(3,1,0,3,-2,5),nrow = 2, ncol = 3)
dim(A) ##El tamaño de la matriz es de 2*3
## [1] 2 3
#2.
A[2,2]
## [1] 3
#3.
B<-A*(3*pi/2)

#4.
B[1,3]
## [1] -9.4248
B
##         [,1]    [,2]    [,3]
## [1,] 14.1372  0.0000 -9.4248
## [2,]  4.7124 14.1372 23.5620
#8. 
length(B)
## [1] 6
#9. 
sum(B[,1])  
## [1] 18.8496
sum(B[,2])
## [1] 14.1372
sum(B[,3])
## [1] 14.1372
min(B[,1])
## [1] 4.7124
min(B[,2])
## [1] 0
min(B[,3])
## [1] -9.4248
max(B[,1])
## [1] 14.1372
max(B[,2])
## [1] 14.1372
max(B[,3])
## [1] 23.562
#10.
a<-c(1,3,0,-4)
b<-c(5,3,1,0)
c<-c(2,2,-1,1)
m<-matrix(data=c(a,b,c), nrow=3,ncol = 4)
m
##      [,1] [,2] [,3] [,4]
## [1,]    1   -4    1    2
## [2,]    3    5    0   -1
## [3,]    0    3    2    1
#11.
R <- matrix(data=c(1,7,3,2,5,1,0,-3,1), ncol=3)
S <- matrix(data=c(1,3,2,3,5,3,-2,7,0), ncol=3)

#12.
R*S
##      [,1] [,2] [,3]
## [1,]    1    6    0
## [2,]   21   25  -21
## [3,]    6    3    0
R/S
##          [,1]      [,2]       [,3]
## [1,] 1.000000 0.6666667  0.0000000
## [2,] 2.333333 1.0000000 -0.4285714
## [3,] 1.500000 0.3333333        Inf
#15-19
x <- matrix(data=c(1,2,-3,5,-2,3,5,-2,0,6,2,4,1,2,1,4), ncol=4, nrow=4)
5+x
##      [,1] [,2] [,3] [,4]
## [1,]    6    3    5    6
## [2,]    7    8   11    7
## [3,]    2   10    7    6
## [4,]   10    3    9    9
x-3
##      [,1] [,2] [,3] [,4]
## [1,]   -2   -5   -3   -2
## [2,]   -1    0    3   -1
## [3,]   -6    2   -1   -2
## [4,]    2   -5    1    1
-3*x
##      [,1] [,2] [,3] [,4]
## [1,]   -3    6    0   -3
## [2,]   -6   -9  -18   -6
## [3,]    9  -15   -6   -3
## [4,]  -15    6  -12  -12
x/2
##      [,1] [,2] [,3] [,4]
## [1,]  0.5 -1.0    0  0.5
## [2,]  1.0  1.5    3  1.0
## [3,] -1.5  2.5    1  0.5
## [4,]  2.5 -1.0    2  2.0
(-3*x)/(2.4+5)
##            [,1]       [,2]       [,3]       [,4]
## [1,] -0.4054054  0.8108108  0.0000000 -0.4054054
## [2,] -0.8108108 -1.2162162 -2.4324324 -0.8108108
## [3,]  1.2162162 -2.0270270 -0.8108108 -0.4054054
## [4,] -2.0270270  0.8108108 -1.6216216 -1.6216216
#20-26
B <- matrix(data=c(180/3,2*180/3,2*180/3,180), ncol=2)
sin(B)
##            [,1]       [,2]
## [1,] -0.3048106  0.5806112
## [2,]  0.5806112 -0.8011526
cos(B)
##           [,1]       [,2]
## [1,] -0.952413  0.8141810
## [2,]  0.814181 -0.5984601
tan(B)
##           [,1]     [,2]
## [1,] 0.3200404 0.713123
## [2,] 0.7131230 1.338690
sqrt(B)
##           [,1]     [,2]
## [1,]  7.745967 10.95445
## [2,] 10.954451 13.41641
library("expm")
## Warning: package 'expm' was built under R version 4.1.2
## Loading required package: Matrix
## 
## Attaching package: 'expm'
## The following object is masked from 'package:Matrix':
## 
##     expm
sqrtm(B)
##                    [,1]                [,2]
## [1,] 4.406405+2.723308i  7.129714-1.683097i
## [2,] 7.129714-1.683097i 11.536119+1.040211i
exp(B)
##              [,1]         [,2]
## [1,] 1.142007e+26 1.304181e+52
## [2,] 1.304181e+52 1.489384e+78
expm(B)
##               [,1]          [,2]
## [1,] 6.661687e+109 1.077884e+110
## [2,] 1.077884e+110 1.744052e+110
log(B)
##          [,1]     [,2]
## [1,] 4.094345 4.787492
## [2,] 4.787492 5.192957
logm(B)
## Warning in sqrt(S[ij, ij]): Se han producido NaNs
## Warning in logm.Higham08(x): NA/NaN from  || Tr - I ||  after 1 step.
## The matrix logarithm may not exist for this matrix.
##      [,1] [,2]
## [1,]  NaN  NaN
## [2,]  NaN  NaN
B<-matrix(data = c(pi/3, 2*pi/3, 2*pi/3, pi), nrow = 2,ncol = 2)
B
##        [,1]   [,2]
## [1,] 1.0472 2.0944
## [2,] 2.0944 3.1416
#27-29
4^B
##           [,1]     [,2]
## [1,]  4.270485 18.23704
## [2,] 18.237044 77.88103
4^B[1,1]
## [1] 4.270485
4^B[1,2]
## [1] 18.23704
4^B[2,1]
## [1] 18.23704
4^B[2,2]
## [1] 77.88103
B^4
##           [,1]     [,2]
## [1,]  1.202593 19.24148
## [2,] 19.241482 97.41000
#30-35
M<-matrix(1:1,nrow = 2,ncol = 3)
N<-matrix(0:0,nrow = 2,ncol = 3)
M<-matrix(data=c(1,0,0,1), nrow=2,ncol=2)
M<-matrix(1:1,nrow = 4,ncol = 4)
N<-matrix(0:0,nrow = 4,ncol = 4)
M<-matrix(data=c(1,0,0,1), nrow=4,ncol=4)

#36-41
C<-matrix(data=c(1,2,1,2,2,5,3,3,-3,2,7,-1,0,-3,-2,3), nrow=4,ncol=4)
C1<-t(C)
C+C1
##      [,1] [,2] [,3] [,4]
## [1,]    2    4   -2    2
## [2,]    4   10    5    0
## [3,]   -2    5   14   -3
## [4,]    2    0   -3    6
diag(C)
## [1] 1 5 7 3
upper.tri(C)
##       [,1]  [,2]  [,3]  [,4]
## [1,] FALSE  TRUE  TRUE  TRUE
## [2,] FALSE FALSE  TRUE  TRUE
## [3,] FALSE FALSE FALSE  TRUE
## [4,] FALSE FALSE FALSE FALSE
lower.tri(C)
##       [,1]  [,2]  [,3]  [,4]
## [1,] FALSE FALSE FALSE FALSE
## [2,]  TRUE FALSE FALSE FALSE
## [3,]  TRUE  TRUE FALSE FALSE
## [4,]  TRUE  TRUE  TRUE FALSE
det(C)
## [1] -13
D<-solve(C)

#42.
D%*%C #Al multiplicar una matriz por su inversa se obtiene la matriz identidad
##               [,1]          [,2]          [,3]          [,4]
## [1,]  1.000000e+00 -7.105427e-15  2.220446e-15  0.000000e+00
## [2,]  1.332268e-15  1.000000e+00 -3.330669e-15 -8.881784e-16
## [3,] -5.551115e-17 -1.665335e-16  1.000000e+00  8.326673e-17
## [4,]  2.775558e-16  1.110223e-15 -3.608225e-16  1.000000e+00
#43-44
norm(C)
## [1] 13
eigen(C)
## eigen() decomposition
## $values
## [1]  8.19439889+0.000000i  3.93791318+2.661524i  3.93791318-2.661524i
## [4] -0.07022524+0.000000i
## 
## $vectors
##                [,1]                 [,2]                 [,3]           [,4]
## [1,]  0.24942346+0i 0.1235076+0.3224730i 0.1235076-0.3224730i  0.88639724+0i
## [2,] -0.41481971+0i 0.2577553+0.3723022i 0.2577553-0.3723022i -0.44089112+0i
## [3,] -0.87469708+0i 0.3369753-0.1771706i 0.3369753+0.1771706i  0.02228749+0i
## [4,]  0.02485078+0i 0.7285386+0.0000000i 0.7285386+0.0000000i -0.13934927+0i
#45-47
b<-matrix(data=c(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1),nrow = 4,ncol = 4)
b
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    0    0
## [2,]    0    1    0    0
## [3,]    0    0    1    0
## [4,]    0    0    0    1
S<-solve(C,b)
diag(S)
## [1] -10.5384615  -3.0769231   0.0000000  -0.1538462
qr(C)$rank
## [1] 4
matrix(data=c(sample(1:100,25)),nrow = 5,ncol = 5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   60   34   37   86   66
## [2,]   12   38   21   92    1
## [3,]   32   67   76   33    8
## [4,]   14   79   54   80   48
## [5,]   42   25   47   43   40
#48.
M<-matrix(data=c(22,5,30,13,38,21,46,47,23,6,31,14,39,15,16,48,24,7,32,8,40,41,17,49,25,1,33,9,10,42,18,43,26,2,34,35,11,36,19,44,27,3,4,29,12,37,20,45,28),nrow = 7,ncol = 7)
M
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   22   47   16   41   10   35    4
## [2,]    5   23   48   17   42   11   29
## [3,]   30    6   24   49   18   36   12
## [4,]   13   31    7   25   43   19   37
## [5,]   38   14   32    1   26   44   20
## [6,]   21   39    8   33    2   27   45
## [7,]   46   15   40    9   34    3   28
rowSums(M)
## [1] 175 175 175 175 175 175 175
colSums(M)
## [1] 175 175 175 175 175 175 175
#LA SUMA DE CADA FILA Y DE CADA COLUMNA ES IGUAL

Exercises 6

#1.
#Este es un comentario
cost <- 200
sale_price <- 250
profit <- sale_price-cost

#2. 
sphere_vol <- function(r){
  #Funcion para calcular el volumen de una esfera
  3/4*pi*r
}
sphere_vol(2)
## [1] 4.7124
#3. 
RectangleArea <- function(a,b){
  #Calcular area de un rectangulo
  a*b
}
RectangleArea(3,6)
## [1] 18
RectangleArea(2.5,5.5)
## [1] 13.75
#4. 
#Example9
x <- c()
for (i in 1:7){
  x[i] = i^3
}
x
## [1]   1   8  27  64 125 216 343
#5.
#example 10
y <- matrix(ncol=4,nrow = 4)
for (i in 1:4){
  for (j in 1:4){
    y[j,i] = i^2- j^2
  }
}
y
##      [,1] [,2] [,3] [,4]
## [1,]    0    3    8   15
## [2,]   -3    0    5   12
## [3,]   -8   -5    0    7
## [4,]  -15  -12   -7    0
#6.
#Example 11
tol <- 0.0
n <- 3
while(tol <= 1.5){
  n=n+1
  tol= tol+0.1
}
n
## [1] 18
tol
## [1] 1.5
#7.
precio <- function(items){
  if (items>5){
    price=items*160
  }  else {
    price=items*130
  }
  print(paste("Precio:", price))
}
precio(3)
## [1] "Precio: 390"
precio(9)
## [1] "Precio: 1440"
#8.
precio2 <- function (items){
  if (items<5){
    if (items<3){
      price=items*130
  } else {
    price=items*160}
  } 
  else {
    price=items*200
  }
  print(paste("Precio:", price))
}
precio2(2)
## [1] "Precio: 260"
precio2(4)
## [1] "Precio: 640"
precio2(6)
## [1] "Precio: 1200"

Exercises 7

#1.
x=c("1","2","3","4","5","6","7")
y=c(10,15,23,43,30,10,12)

plot(x,y)

#2.
plot(x,y,xlab = "eje X",ylab = "eje Y")

#3.
x<-seq(-6,6)
y<-5+2*x^3

plot(x,y,main="y=2x^3+5")

#4.
plot(x,y,main="y=2x^3+5",type="o",col="blue",lwd=3)

#5.
x<-seq(from=0, to=3*pi, by=pi/4)
x
##  [1] 0.0000 0.7854 1.5708 2.3562 3.1416 3.9270 4.7124 5.4978 6.2832 7.0686
## [11] 7.8540 8.6394 9.4248
y<-c(2*sin(x/3))
z<-c(2*cos(x/3))

plot(x,y,type="p",col="blue",lwd=2,main="grafica",xlab="eje x",ylab="eje y")
lines(x,z,type="b",col="green",lwd=2)
legend("bottomleft",col=c("blue","green"),legend =c("2*sin(x/3)","2*cos(x/3"), lwd=3, bty = "n")

#6.
x<-c(1:10)
y<-c((2*x^3)-4)
z<-c(x+1)
w<-c(2-sqrt(x))
v<-c((x^2)+3)

plot(x,y)

plot(x,z)

plot(x,w)

plot(x,v)

#7.
x<-c(1:10)
y<-c(1:10)
z<-2*sin(x*y) 
library("plot3D")
## Warning: package 'plot3D' was built under R version 4.1.2
scatter3D(x,y,z)