2

I have the matrix y with variable x:

         x
 [1,]    0
 [2,]    1
 [3,]    0
 [4,]    0
 [5,]    1
 [6,]    1

I selected just values with 1. Now I have a vector z:

 2 5 6

I need match this vector with lines selected with my matrix y. This a example, I have a big data. I tried y[z], but this don't show the rows. Thanks

4

1 回答 1

3

y[z,]返回y带有行的矩阵z

y[z]返回z矩阵的元素y

> y <- matrix(1:12, ncol=3)
> y
     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12
> y[c(2,3),]
     [,1] [,2] [,3]
[1,]    2    6   10
[2,]    3    7   11
> y[c(2,3)]
[1] 2 3

正如 Joran 指出的那样,如果您使用的是单列矩阵,,drop=FALSE请确保您的输出是矩阵。

于 2013-06-05T18:53:03.440 回答