Contenido

a1=c(1,2,3,1,2)
a2=c(1,2,4,1,2)
a3=c(1,2,1,1,1)
# formar unos vectores
b1=c(1,2)
b2=c(1,1)
# dos vectores
b1=c(2,2)
b2=c(3,2)

Los vectores se pueden unir por columnas o filas

mat1=cbind(a1,a2,a3)

# unir por columnas
cbind(b1,b2)
##      b1 b2
## [1,]  2  3
## [2,]  2  2
# es usual unirlom por el lado de las filas
rbind(b1,b2)
##    [,1] [,2]
## b1    2    2
## b2    3    2
# uniendo por filas
rbind(a1,a2,a3)
##    [,1] [,2] [,3] [,4] [,5]
## a1    1    2    3    1    2
## a2    1    2    4    1    2
## a3    1    2    1    1    1
# colocando nombres
matriz=rbind(a1,a2,a3)
matriz
##    [,1] [,2] [,3] [,4] [,5]
## a1    1    2    3    1    2
## a2    1    2    4    1    2
## a3    1    2    1    1    1
dim(matriz)
## [1] 3 5

Colocando nombres a la matriz

colnames(matriz)=c('a','b','c','d','e')
rownames(matriz)=c('azul','rojo','verde')

Proporciones

proportions(matriz)# igual a 1
     a    b    c    d    e

azul 0.04 0.08 0.12 0.04 0.08 rojo 0.04 0.08 0.16 0.04 0.08 verde 0.04 0.08 0.04 0.04 0.04

proportions(matriz,margin=1)# rows filas igual a 1
          a         b         c         d         e

azul 0.1111111 0.2222222 0.3333333 0.1111111 0.2222222 rojo 0.1000000 0.2000000 0.4000000 0.1000000 0.2000000 verde 0.1666667 0.3333333 0.1666667 0.1666667 0.1666667

proportions(matriz,margin=2)# por columnaas
          a         b     c         d   e

azul 0.3333333 0.3333333 0.375 0.3333333 0.4 rojo 0.3333333 0.3333333 0.500 0.3333333 0.4 verde 0.3333333 0.3333333 0.125 0.3333333 0.2

Operacione con matrices

matrix(9,ncol=3)
##      [,1] [,2] [,3]
## [1,]    9    9    9
matrix(1:9,ncol=3)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
matriz1=matrix(round(runif(9,1,4),1),ncol=3)
matriz1
##      [,1] [,2] [,3]
## [1,]  3.9  1.2  2.0
## [2,]  1.4  2.5  3.3
## [3,]  1.5  2.8  3.4
# 
matriz1=matrix(round(runif(9,1,4),0),ncol=3)
matriz1
##      [,1] [,2] [,3]
## [1,]    2    3    4
## [2,]    1    3    3
## [3,]    2    4    1
# inversa
solve(matriz1)
##            [,1]       [,2]       [,3]
## [1,]  0.8181818 -1.1818182  0.2727273
## [2,] -0.4545455  0.5454545  0.1818182
## [3,]  0.1818182  0.1818182 -0.2727273
# determinante
det(matriz1)
## [1] -11
# suma de filas y columnas
rowSums(matriz1)
## [1] 9 7 7
colSums(matriz1)
## [1]  5 10  8
# media de filas y columnas
rowMeans(matriz1)
## [1] 3.000000 2.333333 2.333333
colMeans(matriz1)
## [1] 1.666667 3.333333 2.666667
# multiplicacion por un vector y solucion de ecuaciones
r=c(1,1,1)
# multiplicacion por un vector
matriz1%*%r
##      [,1]
## [1,]    9
## [2,]    7
## [3,]    7
# ecuaciones de primer grado
solve(matriz1,r)
## [1] -0.09090909  0.27272727  0.09090909

Subconjuntos de matrices

datos=matrix(round(runif(25)),ncol = 5)
datos[,5]
## [1] 1 1 1 1 0
# solo la fila dos
datos[2,]
## [1] 1 0 1 0 1
# si quiero la columna 1 y la 5
###
datos[,c(1,5)]
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    1
## [3,]    0    1
## [4,]    1    1
## [5,]    0    0
# quitar la columna 1 y 5, colocar un signo menos
datos1=datos[,-c(1,5)]
datos1
##      [,1] [,2] [,3]
## [1,]    0    1    0
## [2,]    0    1    0
## [3,]    1    1    1
## [4,]    1    0    0
## [5,]    1    1    1
datos[2,3:5]
## [1] 1 0 1