#Matrices
A=matrix(c(1,2,3,
4,5,6),
nrow=2,
byrow=TRUE)
print(A)
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
dim(A)#Dimensión de la matrix
## [1] 2 3
#extrer celdas
A[1,2]
## [1] 2
A[,2]#Columna total de 2
## [1] 2 5
A[1,]#Fila total de 1
## [1] 1 2 3
#Suma de columas de matrices
rowSums(A)# suma de filas
## [1] 6 15
rowSums(t(A))#Transpuesta de una matriz(Columnas)
## [1] 5 7 9
colSums(A)#Suma de columnas
## [1] 5 7 9
colSums(t(A))#Transpuesta de una matriz(filas)
## [1] 6 15
#promedios
rowMeans(A)
## [1] 2 5
colMeans(A)
## [1] 2.5 3.5 4.5
#Estructura
str(A)
## num [1:2, 1:3] 1 4 2 5 3 6
####Ejemplo 1
## Matrice tipo 3D (array)
1:24# secuencia de datos del 1 hasta 24
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
B=array(1:24,dim = c(2,3,4))#filas, columnas, bloques
print(B)
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 7 9 11
## [2,] 8 10 12
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 13 15 17
## [2,] 14 16 18
##
## , , 4
##
## [,1] [,2] [,3]
## [1,] 19 21 23
## [2,] 20 22 24
dim(B)
## [1] 2 3 4
B[1, , ]# todas las filas
## [,1] [,2] [,3] [,4]
## [1,] 1 7 13 19
## [2,] 3 9 15 21
## [3,] 5 11 17 23
B[ ,1, ]#Totas las columnas
## [,1] [,2] [,3] [,4]
## [1,] 1 7 13 19
## [2,] 2 8 14 20
B[ , ,1]# bloque Uno
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
#nombras dimensiones
dimnames(B)=list(
Producto=c("Producto 1", "Producto 2"),#filas
Mes=c("Enero", "Febrero", "Marzo"),# columnas
Tienda=c("Tienda 1", "Tienda 2", "Tienda 3", "Tienda 4")#bloques
)
print(B)
## , , Tienda = Tienda 1
##
## Mes
## Producto Enero Febrero Marzo
## Producto 1 1 3 5
## Producto 2 2 4 6
##
## , , Tienda = Tienda 2
##
## Mes
## Producto Enero Febrero Marzo
## Producto 1 7 9 11
## Producto 2 8 10 12
##
## , , Tienda = Tienda 3
##
## Mes
## Producto Enero Febrero Marzo
## Producto 1 13 15 17
## Producto 2 14 16 18
##
## , , Tienda = Tienda 4
##
## Mes
## Producto Enero Febrero Marzo
## Producto 1 19 21 23
## Producto 2 20 22 24
#Consultas
B["Producto 1", "Marzo", "Tienda 2"]
## [1] 11
B["Producto 1", , ]
## Tienda
## Mes Tienda 1 Tienda 2 Tienda 3 Tienda 4
## Enero 1 7 13 19
## Febrero 3 9 15 21
## Marzo 5 11 17 23
B[,"Marzo", ]
## Tienda
## Producto Tienda 1 Tienda 2 Tienda 3 Tienda 4
## Producto 1 5 11 17 23
## Producto 2 6 12 18 24
B[, ,"Tienda 1"]
## Mes
## Producto Enero Febrero Marzo
## Producto 1 1 3 5
## Producto 2 2 4 6
# funciones apply
apply(B,1, sd)# (Fila=1) el promedio de los productos
## Producto 1 Producto 2
## 7.211103 7.211103
apply(B,2, sum)# (columna=2) el promedio de los meses
## Enero Febrero Marzo
## 84 100 116
apply(B,3, mean)# (Bloque=3)el promedio de las tiendas
## Tienda 1 Tienda 2 Tienda 3 Tienda 4
## 3.5 9.5 15.5 21.5
apply(B,c(1,2),mean)# dos dimenasiones
## Mes
## Producto Enero Febrero Marzo
## Producto 1 10 12 14
## Producto 2 11 13 15
apply(B,c(1,3),median)
## Tienda
## Producto Tienda 1 Tienda 2 Tienda 3 Tienda 4
## Producto 1 3 9 15 21
## Producto 2 4 10 16 22
apply(B,c(2,3),max)
## Tienda
## Mes Tienda 1 Tienda 2 Tienda 3 Tienda 4
## Enero 2 8 14 20
## Febrero 4 10 16 22
## Marzo 6 12 18 24
apply(B,c(3,1),min)
## Producto
## Tienda Producto 1 Producto 2
## Tienda 1 1 2
## Tienda 2 7 8
## Tienda 3 13 14
## Tienda 4 19 20
####Ejemplo 2
C=array(101:160,
dim=c(3,4,5))
print(C)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 101 104 107 110
## [2,] 102 105 108 111
## [3,] 103 106 109 112
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 113 116 119 122
## [2,] 114 117 120 123
## [3,] 115 118 121 124
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 125 128 131 134
## [2,] 126 129 132 135
## [3,] 127 130 133 136
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 137 140 143 146
## [2,] 138 141 144 147
## [3,] 139 142 145 148
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 149 152 155 158
## [2,] 150 153 156 159
## [3,] 151 154 157 160
dimnames(C)=list(
Productos=c("Producto A", "Producto B", "Producto C"),
Mes=c("Enero", "Febrero", "Marzo", "Abril"),
Tienda=c("TA", "TB", "TC", "TD" , "TE")
)
print(C)
## , , Tienda = TA
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 101 104 107 110
## Producto B 102 105 108 111
## Producto C 103 106 109 112
##
## , , Tienda = TB
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 113 116 119 122
## Producto B 114 117 120 123
## Producto C 115 118 121 124
##
## , , Tienda = TC
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 125 128 131 134
## Producto B 126 129 132 135
## Producto C 127 130 133 136
##
## , , Tienda = TD
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 137 140 143 146
## Producto B 138 141 144 147
## Producto C 139 142 145 148
##
## , , Tienda = TE
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 149 152 155 158
## Producto B 150 153 156 159
## Producto C 151 154 157 160
apply(C,
c(1,2),
mean)
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 125 128 131 134
## Producto B 126 129 132 135
## Producto C 127 130 133 136
apply(C,
c(2,1),
mean)
## Productos
## Mes Producto A Producto B Producto C
## Enero 125 126 127
## Febrero 128 129 130
## Marzo 131 132 133
## Abril 134 135 136
apply(C,
c(2,3),
mean)
## Tienda
## Mes TA TB TC TD TE
## Enero 102 114 126 138 150
## Febrero 105 117 129 141 153
## Marzo 108 120 132 144 156
## Abril 111 123 135 147 159
apply(C,
c(3,2),
mean)
## Mes
## Tienda Enero Febrero Marzo Abril
## TA 102 105 108 111
## TB 114 117 120 123
## TC 126 129 132 135
## TD 138 141 144 147
## TE 150 153 156 159
apply(C,
c(3,1),
mean)
## Productos
## Tienda Producto A Producto B Producto C
## TA 105.5 106.5 107.5
## TB 117.5 118.5 119.5
## TC 129.5 130.5 131.5
## TD 141.5 142.5 143.5
## TE 153.5 154.5 155.5
apply(C,
c(1,3),
mean)
## Tienda
## Productos TA TB TC TD TE
## Producto A 105.5 117.5 129.5 141.5 153.5
## Producto B 106.5 118.5 130.5 142.5 154.5
## Producto C 107.5 119.5 131.5 143.5 155.5
print(C)
## , , Tienda = TA
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 101 104 107 110
## Producto B 102 105 108 111
## Producto C 103 106 109 112
##
## , , Tienda = TB
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 113 116 119 122
## Producto B 114 117 120 123
## Producto C 115 118 121 124
##
## , , Tienda = TC
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 125 128 131 134
## Producto B 126 129 132 135
## Producto C 127 130 133 136
##
## , , Tienda = TD
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 137 140 143 146
## Producto B 138 141 144 147
## Producto C 139 142 145 148
##
## , , Tienda = TE
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 149 152 155 158
## Producto B 150 153 156 159
## Producto C 151 154 157 160
#Crear un data frame
df=as.data.frame.table(C,
responseName = "Precio")
#View(df)
dim(df)
## [1] 60 4
str(df)
## 'data.frame': 60 obs. of 4 variables:
## $ Productos: Factor w/ 3 levels "Producto A","Producto B",..: 1 2 3 1 2 3 1 2 3 1 ...
## $ Mes : Factor w/ 4 levels "Enero","Febrero",..: 1 1 1 2 2 2 3 3 3 4 ...
## $ Tienda : Factor w/ 5 levels "TA","TB","TC",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Precio : int 101 102 103 104 105 106 107 108 109 110 ...
# 20 primero datos
head(df, 20)
## Productos Mes Tienda Precio
## 1 Producto A Enero TA 101
## 2 Producto B Enero TA 102
## 3 Producto C Enero TA 103
## 4 Producto A Febrero TA 104
## 5 Producto B Febrero TA 105
## 6 Producto C Febrero TA 106
## 7 Producto A Marzo TA 107
## 8 Producto B Marzo TA 108
## 9 Producto C Marzo TA 109
## 10 Producto A Abril TA 110
## 11 Producto B Abril TA 111
## 12 Producto C Abril TA 112
## 13 Producto A Enero TB 113
## 14 Producto B Enero TB 114
## 15 Producto C Enero TB 115
## 16 Producto A Febrero TB 116
## 17 Producto B Febrero TB 117
## 18 Producto C Febrero TB 118
## 19 Producto A Marzo TB 119
## 20 Producto B Marzo TB 120
aggregate(Precio~Productos,
data=df,
mean)
## Productos Precio
## 1 Producto A 129.5
## 2 Producto B 130.5
## 3 Producto C 131.5
aggregate(Precio~Productos,
data=df,
var)
## Productos Precio
## 1 Producto A 315
## 2 Producto B 315
## 3 Producto C 315
aggregate(Precio~Productos,
data=df,
sd)
## Productos Precio
## 1 Producto A 17.74824
## 2 Producto B 17.74824
## 3 Producto C 17.74824
aggregate(Precio~Mes,
data=df,
mean)
## Mes Precio
## 1 Enero 126
## 2 Febrero 129
## 3 Marzo 132
## 4 Abril 135
#Ejemplo 3
set.seed(1234)#raiz
#rnorm(n, media, sd)
D=round(rnorm(72, 120, 5),2)#datos aleatorios
#Creación de los datos
Ventas=array(D,
dim=c(3,4,6))#3*4*5=60
print(Ventas)
## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 113.96 108.27 117.13 115.55
## [2,] 121.39 122.15 117.27 117.61
## [3,] 125.42 122.53 117.18 115.01
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 116.12 119.45 115.81 117.55
## [2,] 120.32 117.44 132.08 117.80
## [3,] 124.80 115.44 120.67 122.30
##
## , , 3
##
## [,1] [,2] [,3] [,4]
## [1,] 116.53 114.88 125.51 117.49
## [2,] 112.76 119.92 117.62 111.85
## [3,] 122.87 115.32 116.45 114.16
##
## , , 4
##
## [,1] [,2] [,3] [,4]
## [1,] 109.10 117.67 115.72 115.16
## [2,] 113.30 127.25 118.60 114.46
## [3,] 118.53 114.66 115.03 113.74
##
## , , 5
##
## [,1] [,2] [,3] [,4]
## [1,] 117.38 117.09 119.19 116.13
## [2,] 117.52 114.46 122.82 128.03
## [3,] 110.97 114.93 128.24 114.21
##
## , , 6
##
## [,1] [,2] [,3] [,4]
## [1,] 123.28 116.65 114.31 121.68
## [2,] 132.74 119.96 126.84 120.03
## [3,] 119.83 128.89 126.65 117.72
dimnames(Ventas)=list(
Productos=c("Producto A", "Producto B", "Producto C"),
Mes=c("Enero", "Febrero", "Marzo", "Abril"),
Tienda=c("TA", "TB", "TC",
"TD" , "TE", "TD")
)
print(Ventas)
## , , Tienda = TA
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 113.96 108.27 117.13 115.55
## Producto B 121.39 122.15 117.27 117.61
## Producto C 125.42 122.53 117.18 115.01
##
## , , Tienda = TB
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 116.12 119.45 115.81 117.55
## Producto B 120.32 117.44 132.08 117.80
## Producto C 124.80 115.44 120.67 122.30
##
## , , Tienda = TC
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 116.53 114.88 125.51 117.49
## Producto B 112.76 119.92 117.62 111.85
## Producto C 122.87 115.32 116.45 114.16
##
## , , Tienda = TD
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 109.10 117.67 115.72 115.16
## Producto B 113.30 127.25 118.60 114.46
## Producto C 118.53 114.66 115.03 113.74
##
## , , Tienda = TE
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 117.38 117.09 119.19 116.13
## Producto B 117.52 114.46 122.82 128.03
## Producto C 110.97 114.93 128.24 114.21
##
## , , Tienda = TD
##
## Mes
## Productos Enero Febrero Marzo Abril
## Producto A 123.28 116.65 114.31 121.68
## Producto B 132.74 119.96 126.84 120.03
## Producto C 119.83 128.89 126.65 117.72
#Crear un data frame
df1=as.data.frame.table(Ventas,
responseName = "Precio")
View(df1)
#Promedi total
T1=aggregate(Precio~Mes+Tienda,
data=df,
mean)
mean(T1$Precio)
## [1] 130.5