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

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
主站蜘蛛池模板: 鄢陵县| 平利县| 高平市| 板桥市| 林芝县| 清苑县| 巨野县| 广宁县| 桑植县| 南溪县| 沙湾县| 浦江县| 卢龙县| 云南省| 山阴县| 沙坪坝区| 门源| 阿拉善右旗| 建湖县| 祁连县| 耿马| 枝江市| 北碚区| 鄂尔多斯市| 英吉沙县| 准格尔旗| 奉新县| 新源县| 保康县| 阿图什市| 三门峡市| 乌恰县| 罗定市| 栖霞市| 灵璧县| 巢湖市| 大安市| 简阳市| 新野县| 花莲市| 宕昌县|