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

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
主站蜘蛛池模板: 崇礼县| 黎平县| 岫岩| 通江县| 陇南市| 凌海市| 紫金县| 和硕县| 油尖旺区| 富锦市| 利津县| 兴业县| 衡南县| 于都县| 平阳县| 肇东市| 醴陵市| 新巴尔虎右旗| 新巴尔虎左旗| 疏勒县| 涡阳县| 海淀区| 新宁县| 镇康县| 灌云县| 武冈市| 广河县| 会泽县| 托克托县| 南丹县| 内丘县| 双牌县| 东乡族自治县| 阿鲁科尔沁旗| 县级市| 景谷| 鄂伦春自治旗| 延寿县| 睢宁县| 嘉善县| 肥乡县|