#150 Ejerciciosde r#
#Maria Alejandra Molina Berbeo#
#Clase de multivariados#
3*4+6
## [1] 18
cos(5)#da en readianes
## [1] 0.2836622
x=4
3*sqrt(6+x)
## [1] 9.486833
y=5.2
x=3
y=4
z=2*x-7*y;z
## [1] -22
z
## [1] -22
A=matrix(c(1,0,2,-3,0,5,2,2,1,2,3,4,-2,0,1,3),nrow = 4,ncol = 4,byrow = T);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=A[3,]
A*b
##      [,1] [,2] [,3] [,4]
## [1,]    1    0    2   -3
## [2,]    0   10    4    4
## [3,]    3    6    9   12
## [4,]   -8    0    4   12
x=matrix(c(1,0,6,1,2,3,4,5,-2), nrow=3,ncol=3,byrow=T);x
##      [,1] [,2] [,3]
## [1,]    1    0    6
## [2,]    1    2    3
## [3,]    4    5   -2
s=subset(x[c(2,3),c(1,2)]);s
##      [,1] [,2]
## [1,]    1    2
## [2,]    4    5
x[,2]
## [1] 0 2 5
x[1,]
## [1] 1 0 6
x[1,3]
## [1] 6
x=(c(1,2,3,4,5,6,7,8,9))
#Cuadro mágico
magic=matrix(c(1,15,14,4,12,6,7,9,8,10,11,5,13,3,2,16), nrow=4,ncol=4,byrow=T);magic
##      [,1] [,2] [,3] [,4]
## [1,]    1   15   14    4
## [2,]   12    6    7    9
## [3,]    8   10   11    5
## [4,]   13    3    2   16
w=2*3+7;w
## [1] 13
a=4
b=-10
c=3.2
y=10#no es igual a la de abajo, es suceptible a mayúsculas
Y=100
x=5.5
y=-2.6
z=(2*x-3*y);z
## [1] 18.8
w=(3*y-z+y*x/y);w
## [1] -21.1
r=6.3
s=5.8
final=r+s-r*s;final
## [1] -24.44
this_is_the_result=r^2-s^2;this_is_the_result
## [1] 6.05
width=1.5# no son iguales
Width=2.0
WIDTH=4.5
#This line will not executed
s=3.5#Para comentario se escribe # y no se ejecuta 
y1=7
y2=9
y3=y1-y2/3;y3
## [1] 4
#2*m-5#genera error porque la variable m no está definida y no la encuentra 
cost=175
profit=25
sale_price=cost+profit;sale_price
## [1] 200
centigrade=28
fahrenheit=(centigrade*9/5);fahrenheit
## [1] 50.4
#PAra que me muestre todas las variables que tengo en la sesión
ls(all.names = T)
##  [1] "a"                  "A"                  "b"                 
##  [4] "c"                  "centigrade"         "cost"              
##  [7] "fahrenheit"         "final"              "magic"             
## [10] "profit"             "r"                  "s"                 
## [13] "sale_price"         "this_is_the_result" "w"                 
## [16] "width"              "Width"              "WIDTH"             
## [19] "x"                  "y"                  "Y"                 
## [22] "y1"                 "y2"                 "y3"                
## [25] "z"
#Borrar todas las varibles que tengo en R
rm(list=ls(all=TRUE)) 
#área y perímtro de rectángulo
y=5
x=7
area=y*x;area
## [1] 35
peri=2*(y)+2*(x);peri
## [1] 24
#área y perímetro de círculo
r=6.45
area_cir=pi*(r^2)
perímetro=2*pi*r
x=4/5
z=14/17
y=2*x-z
radio=2/3
area_cir2=pi*(radio^2)
r2=2/3
t=4/3  
volumen=t*pi*r2
double(area_cir)
##   [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [36] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
##  [71] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [106] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
double(area_cir2)
## [1] 0
y
## [1] 0.7764706
date
## function () 
## .Internal(date())
## <bytecode: 0x000000001c09f370>
## <environment: namespace:base>
sqrt(10)
## [1] 3.162278
factorial(7)
## [1] 5040
cos(45)
## [1] 0.525322
sin(45)
## [1] 0.8509035
tan(45)
## [1] 1.619775
atan(45)
## [1] 1.548578
tan(3*pi/2)
## [1] 5.443746e+15
exp(3)
## [1] 20.08554
log(3.5)
## [1] 1.252763
log10(3.5)
## [1] 0.544068
round(2.43)
## [1] 2
abs(-3.6)
## [1] 3.6
1.5-2*(sqrt(6.7/5))
## [1] -0.8151674
log10(0)
## [1] -Inf
X=3*pi/2
y=2*pi
2*sin(x)*cos(y)
## [1] 1.434712
g=45
r=(sqrt(g))
round(r)
## [1] 7
f=2
exp(pi/2)
## [1] 4.810477
exp(pi/f)
## [1] 4.810477
w=c(2,4,-6,0);w
## [1]  2  4 -6  0
w[2]
## [1] 4
z=pi/2*w;z
## [1]  3.141593  6.283185 -9.424778  0.000000
z[1]
## [1] 3.141593
length(z)
## [1] 4
sum(z[1],z[2],z[3])
## [1] 0
max(z)
## [1] 6.283185
min(z)
## [1] -9.424778
c=c(9,3,-2)
d=c(1,2,4)
cbind(c,d)
##       c d
## [1,]  9 1
## [2,]  3 2
## [3,] -2 4
rbind(c,d)
##   [,1] [,2] [,3]
## c    9    3   -2
## d    1    2    4
f=c(0.2,1.3,-3.5)
h=c(0.5,-2.5,1.0)
j=cbind(f,h)
j[,c(1,2)]
##         f    h
## [1,]  0.2  0.5
## [2,]  1.3 -2.5
## [3,] -3.5  1.0
h*f
## [1]  0.10 -3.25 -3.50
h%%f#para multiplicar término por término
## [1]  0.1  0.1 -2.5
h%*%f
##       [,1]
## [1,] -6.65
#No se pueden unir vectores de diferentes dimensiones
w=c(0.1,1.3,-2.4)
5+w
## [1] 5.1 6.3 2.6
-2*w
## [1] -0.2 -2.6  4.8
1.5*w
## [1]  0.15  1.95 -3.60
w/10
## [1]  0.01  0.13 -0.24
3-2*w/5i
## [1] 3+0.04i 3+0.52i 3-0.96i
b=0*pi/3*2*pi/3*pi
sin(b)
## [1] 0
cos(b)
## [1] 1
exp(b)
## [1] 1
sqrt(b)
## [1] 0
3^b
## [1] 1
v=c(1,2,3,4)#vector de 4 elementos
g=c(1,2,3,4,5,6)#vector de 6 elementos
h=c(0.35,-1.0,-0.24,1.30,-0.03)
sort(h,decreasing = F)
## [1] -1.00 -0.24 -0.03  0.35  1.30
j=c(2,4,-3,0,1,5,7)
mean(j)
## [1] 2.285714
median(j)
## [1] 2
range(j)
## [1] -3  7
x=c("r","s","t","v");x
## [1] "r" "s" "t" "v"
A=matrix(c(3,0,-2,1,3,5),byrow=T,nrow=2);A
##      [,1] [,2] [,3]
## [1,]    3    0   -2
## [2,]    1    3    5
A[2,2]
## [1] 3
A=A*(3*pi/2)
A[1,3]
## [1] -9.424778
dim(A)
## [1] 2 3
length(A)
## [1] 6
unique(A)
##           [,1]     [,2]      [,3]
## [1,] 14.137167  0.00000 -9.424778
## [2,]  4.712389 14.13717 23.561945
min(A[,1])
## [1] 4.712389
max(A[,1])
## [1] 14.13717
a=c(1,3,0,4)
b=c(5,3,1,0)
c=c(2,2,-1,1)
cbind(a,b,c)
##      a b  c
## [1,] 1 5  2
## [2,] 3 3  2
## [3,] 0 1 -1
## [4,] 4 0  1
rbind(a,b,c)
##   [,1] [,2] [,3] [,4]
## a    1    3    0    4
## b    5    3    1    0
## c    2    2   -1    1
R=matrix(c(1,2,0,7,5,-3,3,1,1),nrow = 3,ncol = 3,byrow = T);R
##      [,1] [,2] [,3]
## [1,]    1    2    0
## [2,]    7    5   -3
## [3,]    3    1    1
S=matrix(c(1,3,-2,3,5,7,2,3,0),nrow = 3,ncol = 3,byrow = T);S
##      [,1] [,2] [,3]
## [1,]    1    3   -2
## [2,]    3    5    7
## [3,]    2    3    0
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
X=matrix( c(1,-2,0,1,2,3,6,2,-3,5,2,1,5,-2,4,4),nrow = 4,ncol=4,byrow=T);X
##      [,1] [,2] [,3] [,4]
## [1,]    1   -2    0    1
## [2,]    2    3    6    2
## [3,]   -3    5    2    1
## [4,]    5   -2    4    4
j=X+5;j
##      [,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,] FALSE FALSE FALSE FALSE
## [2,] FALSE  TRUE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE
X*-3
##      [,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,]    2   -4    0    2
## [2,]    4    6   12    4
## [3,]   -6   10    4    2
## [4,]   10   -4    8    8
-3*X/24+55
##        [,1]   [,2]  [,3]   [,4]
## [1,] 54.875 55.250 55.00 54.875
## [2,] 54.750 54.625 54.25 54.750
## [3,] 55.375 54.375 54.75 54.875
## [4,] 54.375 55.250 54.50 54.500
matrix(c(1),nrow = 2,ncol=3)
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    1
matrix(c(0),nrow = 2,ncol=3)
##      [,1] [,2] [,3]
## [1,]    0    0    0
## [2,]    0    0    0
matrix(c(1),nrow = 4,ncol=4)
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    1    1
## [2,]    1    1    1    1
## [3,]    1    1    1    1
## [4,]    1    1    1    1
matrix(c(0),nrow = 4,ncol=4)
##      [,1] [,2] [,3] [,4]
## [1,]    0    0    0    0
## [2,]    0    0    0    0
## [3,]    0    0    0    0
## [4,]    0    0    0    0
c=matrix(c(1,2,-3,0,2,5,2,-3,1,3,7,-2,2,3,-1,3),nrow = 4,byrow = T);c
##      [,1] [,2] [,3] [,4]
## [1,]    1    2   -3    0
## [2,]    2    5    2   -3
## [3,]    1    3    7   -2
## [4,]    2    3   -1    3
  t(c)
##      [,1] [,2] [,3] [,4]
## [1,]    1    2    1    2
## [2,]    2    5    3    3
## [3,]   -3    2    7   -1
## [4,]    0   -3   -2    3
solve(c)
##             [,1]        [,2] [,3]        [,4]
## [1,] -10.5384615  6.38461538   -6  2.38461538
## [2,]   5.3076923 -3.07692308    3 -1.07692308
## [3,]  -0.3076923  0.07692308    0  0.07692308
## [4,]   1.6153846 -1.15384615    1 -0.15384615
c+c
##      [,1] [,2] [,3] [,4]
## [1,]    2    4   -6    0
## [2,]    4   10    4   -6
## [3,]    2    6   14   -4
## [4,]    4    6   -2    6
diag(c)
## [1] 1 5 7 3
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
  c[lower.tri(c)]
## [1]  2  1  2  3  3 -1
library(Matrix)
tril(c)
## 4 x 4 Matrix of class "dtrMatrix"
##      [,1] [,2] [,3] [,4]
## [1,]    1    .    .    .
## [2,]    2    5    .    .
## [3,]    1    3    7    .
## [4,]    2    3   -1    3
triu(c)
## 4 x 4 Matrix of class "dtrMatrix"
##      [,1] [,2] [,3] [,4]
## [1,]    1    2   -3    0
## [2,]    .    5    2   -3
## [3,]    .    .    7   -2
## [4,]    .    .    .    3
sum(diag(c))
## [1] 16
determinant(c)
## $modulus
## [1] 2.564949
## attr(,"logarithm")
## [1] TRUE
## 
## $sign
## [1] -1
## 
## attr(,"class")
## [1] "det"
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]
## [1,]  0.24942346+0i 0.1235076+0.3224730i 0.1235076-0.3224730i
## [2,] -0.41481971+0i 0.2577553+0.3723022i 0.2577553-0.3723022i
## [3,] -0.87469708+0i 0.3369753-0.1771706i 0.3369753+0.1771706i
## [4,]  0.02485078+0i 0.7285386+0.0000000i 0.7285386+0.0000000i
##                [,4]
## [1,]  0.88639724+0i
## [2,] -0.44089112+0i
## [3,]  0.02228749+0i
## [4,] -0.13934927+0i
range(c)
## [1] -3  7
#Cálcula de costos
cost=200
sale_price=250
profit=sale_price-cost
#Volumen de una esfera
n=4/3
p=3.1416
vol.m=function(r,n,p){
  
  n+pi^3
#Ärea de rectangulo
  a=3
  b=6
  
}
are_rec=function(a,b){
  a*b
}
are_rec(3,6)
## [1] 18
are_rec(2.5,5.5)
## [1] 13.75
x=c(1,2,3,4,5,6,7)
y=c(10,15,23,43,30,10,12)
k=plot(x~y,xlab="x",ylab="y",lwd=3)
abline(k)

y=2*sin(x/3);y
## [1] 0.6543894 1.2367396 1.6829420 1.9438758 1.9908159 1.8185949 1.4461718
z=2*cos(x/3)
x=c(0,1,2,3)
par(mfrow=c(1,2))
plot(y)
y=2*x^3-4
z=x+1
w=2-sqrt(x)
v=x^2+3
x=c(1,2,3,4,5,6,7,8,9,10)
par(mfrow=c(2,2))

plot(y)
plot(z)
plot(w)
plot(v)

M=matrix(c(0.1,0.2,0.5,0.7,0.3,0.2,0.8,0.5,0.6,0.3,0.5,0.9,0.9,0.4,0.4,0.4,
           0.4,0.5,0.7,0.9,0.1,0.3,0.4,0.6,0.8),byrow =T,ncol=5 );M
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  0.1  0.2  0.5  0.7  0.3
## [2,]  0.2  0.8  0.5  0.6  0.3
## [3,]  0.5  0.9  0.9  0.4  0.4
## [4,]  0.4  0.4  0.5  0.7  0.9
## [5,]  0.1  0.3  0.4  0.6  0.8