3-2 矩阵的子集

> x <- matrix(1:6,nrow=2,ncol=3)
> x
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6


> x[1,2]
[1] 3


> x[2,3]
[1] 6


> x[1,] #第一行的内容
[1] 1 3 5


> x[,1] #第一列的内容
[1] 1 2


> x[2,c(2,3)] #第二行的第2和第3个元素
[1] 4 6


> class(x[1,2])
[1] "integer"


> x[1,2,drop=FALSE]
  [,1]
[1,] 3


> x[1,2,drop=TRUE]
[1] 3

猜你喜欢

转载自www.cnblogs.com/hankleo/p/9942202.html