March 2019
Beginner to intermediate
250 pages
6h 6m
English
A matrix can be created by rows instead of by columns, which is done by using the byrow parameter, as follows:
m<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=3,byrow=TRUE)m## [,1] [,2] [,3]## [1,] 1 2 3## [2,] 4 5 6
With the dimnames parameter, column names can be added to the matrix:
m<-matrix(c(1,2,3,4,5,6), nrow=2, ncol=3,byrow=TRUE,dimnames=list(c('Obs1', 'Obs2'), c('col1', 'Col2','Col3')))m## col1 Col2 Col3## Obs1 1 2 3## Obs2 4 5 6
There are three more alternatives to creating matrices:
rbind(1:3,4:6,10:12)## [,1] [,2] [,3]## [1,] 1 2 3## [2,] 4 5 6## [3,] 10 11 12cbind(1:3,4:6,10:12)## [,1] [,2] [,3]## [1,] 1 4 10## [2,] 2 5 11## [3,] 3 6 12m<-array(c(1,2,3,4,5,6), dim=c(2,3))m## [,1] [,2] [,3]## [1,] 1 3 5## [2,] 2 4 6
Read now
Unlock full access