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

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
主站蜘蛛池模板: 咸宁市| 彰化市| 镶黄旗| 广平县| 年辖:市辖区| 东海县| 闵行区| 元江| 讷河市| 苍南县| 水城县| 福州市| 班戈县| 温泉县| 太谷县| 石台县| 绥江县| 拜城县| 囊谦县| 玛多县| 台中县| 永德县| 东兴市| 拉萨市| 安平县| 孟村| 西宁市| 甘孜县| 正阳县| 朝阳县| 克什克腾旗| 友谊县| 怀来县| 镇雄县| 勃利县| 黎平县| 湖口县| 山西省| 汕尾市| 九寨沟县| 滁州市|