官术网_书友最值得收藏!

Creating matrices

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 12
cbind(1:3,4:6,10:12)
## [,1] [,2] [,3]
## [1,] 1 4 10
## [2,] 2 5 11
## [3,] 3 6 12
m<-array(c(1,2,3,4,5,6), dim=c(2,3))
m
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
主站蜘蛛池模板: 高阳县| 叶城县| 木兰县| 青浦区| 铜川市| 三江| 睢宁县| 西和县| 丰镇市| 云梦县| 平果县| 米脂县| 长治市| 防城港市| 武山县| 巴林左旗| 东阿县| 宿州市| 竹山县| 汕尾市| 五家渠市| 无锡市| 贡山| 乌拉特后旗| 阳江市| 闵行区| 深泽县| 海晏县| 兰西县| 景谷| 龙南县| 四平市| 开江县| 安阳市| 磐石市| 宁城县| 迭部县| 抚远县| 玛多县| 鄂托克旗| 济阳县|