\(Taller\)
#Ejercicio 1
prod<-function(k){
producto=1
for(i in seq(k)){
p_i=(4*i^2)/((2*i^2)-1)
producto=producto*p_1
}
return(producto)
}
######Ejercicio 5
####Vector1
sec<-seq(from=3, to=6,by=0.1)
vector=c(2.71828^sec* cos(sec))
#Vector 2
i=NULL; ope=NULL; vector2=NULL
for(i in 1:25){ope<-2^i/i; vector2<-c(vector2,ope)}
round(vector2,2)
## [1] 2.00 2.00 2.67 4.00 6.40 10.67
## [7] 18.29 32.00 56.89 102.40 186.18 341.33
## [13] 630.15 1170.29 2184.53 4096.00 7710.12 14563.56
## [19] 27594.11 52428.80 99864.38 190650.18 364722.09 699050.67
## [25] 1342177.28
#Vector 3
paste("Tratamiento", 1:30)
## [1] "Tratamiento 1" "Tratamiento 2" "Tratamiento 3" "Tratamiento 4"
## [5] "Tratamiento 5" "Tratamiento 6" "Tratamiento 7" "Tratamiento 8"
## [9] "Tratamiento 9" "Tratamiento 10" "Tratamiento 11" "Tratamiento 12"
## [13] "Tratamiento 13" "Tratamiento 14" "Tratamiento 15" "Tratamiento 16"
## [17] "Tratamiento 17" "Tratamiento 18" "Tratamiento 19" "Tratamiento 20"
## [21] "Tratamiento 21" "Tratamiento 22" "Tratamiento 23" "Tratamiento 24"
## [25] "Tratamiento 25" "Tratamiento 26" "Tratamiento 27" "Tratamiento 28"
## [29] "Tratamiento 29" "Tratamiento 30"
#Vector 4
paste("Gen", 1:10, sep = "")
## [1] "Gen1" "Gen2" "Gen3" "Gen4" "Gen5" "Gen6" "Gen7" "Gen8" "Gen9"
## [10] "Gen10"
#Vector 5
v5=replicate(n = 20, rnorm(40, 3, 0.3) )
med=colMeans(v5)
#Vector 6
de=apply(v5, 2, sd)
#Vector 7
cv=de/med
######Ejercicio 6
ma<-c(1,1,3,2,9,3,-4,5,1)#Con esta no funcionó
datosma2<-c(1,2,-4,1,9,5,3,3,1)
mat2<-matrix(datosma2, nrow = 3, ncol = 3)
#Inversa de la matriz
solve(mat2)
## [,1] [,2] [,3]
## [1,] -0.05084746 0.11864407 -0.20338983
## [2,] -0.11864407 0.11016949 0.02542373
## [3,] 0.38983051 -0.07627119 0.05932203
#Matriz transpuesta
t(mat2)
## [,1] [,2] [,3]
## [1,] 1 2 -4
## [2,] 1 9 5
## [3,] 3 3 1
#Determinante matriz
det(mat2)
## [1] 118
#Traza
sum(diag(mat2))
## [1] 11
#Rango
qr(mat2)$rank
## [1] 3
#Dimensión
dim(mat2)
## [1] 3 3
#Multiplicación con *-->Útil elemento a elemento por ella misma o un escalar
mat2*mat2
## [,1] [,2] [,3]
## [1,] 1 1 9
## [2,] 4 81 9
## [3,] 16 25 1
#Multiplicación con %*%
mat2%*%mat2
## [,1] [,2] [,3]
## [1,] -9 25 9
## [2,] 8 98 36
## [3,] 2 46 4
# Multiplicación: A^3
mat2*mat2*mat2
## [,1] [,2] [,3]
## [1,] 1 1 27
## [2,] 8 729 27
## [3,] -64 125 1
mat2%*%mat2%*%mat2
## [,1] [,2] [,3]
## [1,] 5 261 57
## [2,] 60 1070 354
## [3,] 78 436 148
#Nota: Con * cada término (ubicado en una columna y fila específico) de la matriz A se multiplica con el término (ubicado en la misma fila y columna ) de B
# Con %*% se multiplica cada fila por columna
#Matriz
datmatrix<-c(0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,1,0)
mat3<-matrix(datmatrix, nrow = 6, ncol = 6)
row(mat3)#asigna a cada fila de la matriz un número de manera ascendente
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 1 1 1
## [2,] 2 2 2 2 2 2
## [3,] 3 3 3 3 3 3
## [4,] 4 4 4 4 4 4
## [5,] 5 5 5 5 5 5
## [6,] 6 6 6 6 6 6
col(mat3)#asigna a cada columna de la matriz un número de manera ascendente
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 2 3 4 5 6
## [2,] 1 2 3 4 5 6
## [3,] 1 2 3 4 5 6
## [4,] 1 2 3 4 5 6
## [5,] 1 2 3 4 5 6
## [6,] 1 2 3 4 5 6
nrow(mat3)#Número de filas
## [1] 6
NROW(mat3)#Igual que nrow
## [1] 6
NCOL(mat3)#Número de columnas
## [1] 6
ncol(mat3)#Igual que NCOL
## [1] 6
anyNA(mat3)#Identificar si hay valores no disponibles
## [1] FALSE
as.matrix(mat3)#convertir datos en una matriz
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 1 0 0 0 0
## [2,] 1 0 1 0 0 0
## [3,] 0 1 0 1 0 0
## [4,] 0 0 1 0 1 0
## [5,] 0 0 0 1 0 1
## [6,] 0 0 0 0 1 0
as.raw(mat3)
## [1] 00 01 00 00 00 00 01 00 01 00 00 00 00 01 00 01 00 00 00 00 01 00 01 00 00
## [26] 00 00 01 00 01 00 00 00 00 01 00
colnames(mat3)#debería dar el nombre de cada columna de mi matriz
## NULL
colSums(mat3)#Sumatoria de cada columna de mi matriz
## [1] 1 2 2 2 2 1
cummax(mat3)#reemplaza por el valor máximo de los datos, el primer cero no se reemplaza porque es por decirlo así el primer máximo, luego 1 es el nuevo máximo entonces de ahí para adelante si reemplaza el cero por 1
## [1] 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
cummin(mat3)#reemplaza por el mínimo de los datos
## [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 0
cumprod(mat3)#multiplicación acumulativa
## [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 0
cumsum(mat3)#suma acumulativa
## [1] 0 1 1 1 1 1 2 2 3 3 3 3 3 4 4 5 5 5 5 5 6 6 7 7 7
## [26] 7 7 8 8 9 9 9 9 9 10 10
det(mat3)
## [1] -1
diag(mat3)
## [1] 0 0 0 0 0 0
diff(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 -1 1 0 0 0
## [2,] -1 1 -1 1 0 0
## [3,] 0 -1 1 -1 1 0
## [4,] 0 0 -1 1 -1 1
## [5,] 0 0 0 -1 1 -1
dimnames(mat3)
## NULL
exp(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1.000000 2.718282 1.000000 1.000000 1.000000 1.000000
## [2,] 2.718282 1.000000 2.718282 1.000000 1.000000 1.000000
## [3,] 1.000000 2.718282 1.000000 2.718282 1.000000 1.000000
## [4,] 1.000000 1.000000 2.718282 1.000000 2.718282 1.000000
## [5,] 1.000000 1.000000 1.000000 2.718282 1.000000 2.718282
## [6,] 1.000000 1.000000 1.000000 1.000000 2.718282 1.000000
is.matrix(mat3)
## [1] TRUE
is.unsorted(mat3)
## [1] TRUE
is.vector(mat3)
## [1] FALSE
isSymmetric(mat3)
## [1] TRUE
length(mat3)
## [1] 36
lengths(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 1 1 1
## [2,] 1 1 1 1 1 1
## [3,] 1 1 1 1 1 1
## [4,] 1 1 1 1 1 1
## [5,] 1 1 1 1 1 1
## [6,] 1 1 1 1 1 1
log10(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -Inf 0 -Inf -Inf -Inf -Inf
## [2,] 0 -Inf 0 -Inf -Inf -Inf
## [3,] -Inf 0 -Inf 0 -Inf -Inf
## [4,] -Inf -Inf 0 -Inf 0 -Inf
## [5,] -Inf -Inf -Inf 0 -Inf 0
## [6,] -Inf -Inf -Inf -Inf 0 -Inf
lower.tri(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] FALSE FALSE FALSE FALSE FALSE FALSE
## [2,] TRUE FALSE FALSE FALSE FALSE FALSE
## [3,] TRUE TRUE FALSE FALSE FALSE FALSE
## [4,] TRUE TRUE TRUE FALSE FALSE FALSE
## [5,] TRUE TRUE TRUE TRUE FALSE FALSE
## [6,] TRUE TRUE TRUE TRUE TRUE FALSE
margin.table(mat3)
## [1] 10
marginSums(mat3)
## [1] 10
max(mat3)
## [1] 1
max.col(mat3)
## [1] 2 1 2 5 4 5
mean(mat3)
## [1] 0.2777778
min(mat3)
## [1] 0
nchar(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 1 1 1 1 1
## [2,] 1 1 1 1 1 1
## [3,] 1 1 1 1 1 1
## [4,] 1 1 1 1 1 1
## [5,] 1 1 1 1 1 1
## [6,] 1 1 1 1 1 1
norm(mat3)
## [1] 2
order(mat3)
## [1] 1 3 4 5 6 8 10 11 12 13 15 17 18 19 20 22 24 25 26 27 29 31 32 33 34
## [26] 36 2 7 9 14 16 21 23 28 30 35
pmax(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 1 0 0 0 0
## [2,] 1 0 1 0 0 0
## [3,] 0 1 0 1 0 0
## [4,] 0 0 1 0 1 0
## [5,] 0 0 0 1 0 1
## [6,] 0 0 0 0 1 0
pmin(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 1 0 0 0 0
## [2,] 1 0 1 0 0 0
## [3,] 0 1 0 1 0 0
## [4,] 0 0 1 0 1 0
## [5,] 0 0 0 1 0 1
## [6,] 0 0 0 0 1 0
range(mat3)
## [1] 0 1
rank(mat3)
## [1] 13.5 31.5 13.5 13.5 13.5 13.5 31.5 13.5 31.5 13.5 13.5 13.5 13.5 31.5 13.5
## [16] 31.5 13.5 13.5 13.5 13.5 31.5 13.5 31.5 13.5 13.5 13.5 13.5 31.5 13.5 31.5
## [31] 13.5 13.5 13.5 13.5 31.5 13.5
solve(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 1 0 -1 0 1
## [2,] 1 0 0 0 0 0
## [3,] 0 0 0 1 0 -1
## [4,] -1 0 1 0 0 0
## [5,] 0 0 0 0 0 1
## [6,] 1 0 -1 0 1 0
sort(mat3)
## [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 1 1 1 1 1 1 1 1 1 1
sum(mat3)
## [1] 10
t(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0 1 0 0 0 0
## [2,] 1 0 1 0 0 0
## [3,] 0 1 0 1 0 0
## [4,] 0 0 1 0 1 0
## [5,] 0 0 0 1 0 1
## [6,] 0 0 0 0 1 0
upper.tri(mat3)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] FALSE TRUE TRUE TRUE TRUE TRUE
## [2,] FALSE FALSE TRUE TRUE TRUE TRUE
## [3,] FALSE FALSE FALSE TRUE TRUE TRUE
## [4,] FALSE FALSE FALSE FALSE TRUE TRUE
## [5,] FALSE FALSE FALSE FALSE FALSE TRUE
## [6,] FALSE FALSE FALSE FALSE FALSE FALSE
which.max(mat3)
## [1] 2
pH=c(6.12,5.13,5.84,6.53,6.12,6.30,6.04,5.79,5.94,6.03,6.12)
tiempo= seq(from=30, to=330,by=30)
plot(tiempo,pH, pch=17,col="darkviolet", type="b")
grid(lty=5, col="orange")
text(90,5.2,"θ =1.34")
#Cálculo de pendiente
tiempotramo=c(30,60,90)
pHtramo=c(5.13,5.84,6.53)
mod=lm(pHtramo~tiempotramo)
theta=atan(mod$coefficients[2 ])
pendiente=theta*180/pi
#Ejericio 12
pp<-rbeta(30,0.8,0.5)
datos<-data.frame(pp)#Data.frame crear tabla a partir de rbeta
ppmult<-datos*100
mpp=mean(ppmult)
## Warning in mean.default(ppmult): argument is not numeric or logical: returning
## NA
vectors<-c(44.15,82.26,86.66,36.00,98.73,97.78,90.41,77.45,17.22,18.59,99.72,42.94,78.45,42.08,34.41,6.34,58.09,32.05,92.00,57.26,80.77,69.16,16.62,69.97,24.74,16.10,77.26,67.24,6.13,56.03)
desvi<-sd(vectors)
datos_2sd=100*sum(ppmult>(mpp-2*desvi)& ppmult<(mpp+2*desvi))/length(ppmult)
sumacu=cumsum(vectors)
which.max(vectors)
## [1] 11
which.min(vectors)
## [1] 29
#Actividad 13
x<-c(1,2,5,9,11)
y<-c(2,5,1,0,23)
intersect(x,y)#Me da los intersectos entre ambos vectores
## [1] 1 2 5
setdiff(x,y)#devuelve las filas en x que no están en y
## [1] 9 11
setdiff(y,x)#devuelve las filas en y que no están en x
## [1] 0 23
union(x,y)#une ambos vectores
## [1] 1 2 5 9 11 0 23
#Actividad 14
set.seed(123)
td<-rbeta(30,2,1)
datoss<-data.frame(td)#Data.frame crear tabla a partir de rbeta
tdmult<-datoss*25
tdmult[tdmult>20 ]#Valores mayores a 20
## [1] 24.23301 24.44851 20.19500 23.53852 20.42654 21.56602 22.20562 23.61541
## [9] 22.44633 22.37667 22.13464 21.50145 20.76335
mean(tdmult[tdmult>=4])#sacar la media solo con los valores >=4
## [1] 17.33406
tdmult[tdmult==0|tdmult==1]#No me da da
## numeric(0)