Elaborar matrices en r

Para elaborar matrices en el software estadistico R usamos la funcion: matrix()

Veamos algunos ejemplos:

matrix(c(10,20,30,40,50,60),nrow=3,ncol=2)
##      [,1] [,2]
## [1,]   10   40
## [2,]   20   50
## [3,]   30   60
matrix(c(10,20,30,40,50,60),nrow=2,ncol=3)
##      [,1] [,2] [,3]
## [1,]   10   30   50
## [2,]   20   40   60

Suma de matrices

Vamos a crear las matrices m1,m2 y m3

m1<-matrix(c(1:9),nrow=3,ncol=3)
m1
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
m2<-matrix(c(10:18),nrow=3,ncol=3)
m2
##      [,1] [,2] [,3]
## [1,]   10   13   16
## [2,]   11   14   17
## [3,]   12   15   18
m3<-m1+m2
m3
##      [,1] [,2] [,3]
## [1,]   11   17   23
## [2,]   13   19   25
## [3,]   15   21   27
#plot(pressure)

Note

Resta de matrices

m1<-matrix(c(21:29),nrow=3,ncol=3)
m1
##      [,1] [,2] [,3]
## [1,]   21   24   27
## [2,]   22   25   28
## [3,]   23   26   29
m2<-matrix(c(10:18),nrow=3,ncol=3)
m2
##      [,1] [,2] [,3]
## [1,]   10   13   16
## [2,]   11   14   17
## [3,]   12   15   18
m5<-m1-m2
m5
##      [,1] [,2] [,3]
## [1,]   11   11   11
## [2,]   11   11   11
## [3,]   11   11   11

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Combinar dos o mas matrices por columnas

cbind(m1,m2)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]   21   24   27   10   13   16
## [2,]   22   25   28   11   14   17
## [3,]   23   26   29   12   15   18

Combinar dos o mas matrices por filas

rbind(m1,m2)
##      [,1] [,2] [,3]
## [1,]   21   24   27
## [2,]   22   25   28
## [3,]   23   26   29
## [4,]   10   13   16
## [5,]   11   14   17
## [6,]   12   15   18