简体   繁体   English

R通过4个坐标选择矩阵

[英]R select matrix by 4 coordinates

So, for example, I have this matrix in R : 因此,例如,我在R中有这个矩阵:

example <- matrix(1:25, nrow = 5)
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    1    6   11   16   21
#[2,]    2    7   12   17   22
#[3,]    3    8   13   18   23
#[4,]    4    9   14   19   24
#[5,]    5   10   15   20   25

I want to select from example matrix from 2:2 to 4:4. 我想从2:2到4:4的示例矩阵中进行选择。 How can I do this? 我怎样才能做到这一点? Expected result is: 预期结果是:

#     [,1] [,2] [,3]
#[1,]    7   12   17
#[2,]    8   13   18
#[3,]    9   14   19

We can use row/column index, ie selecting rows from 2nd to 4th and columns from 2nd to 4th. 我们可以使用行/列索引,即从第二到第四选择行,从第二到第四选择列。

example[2:4, 2:4]
#     [,1] [,2] [,3]
#[1,]    7   12   17
#[2,]    8   13   18
#[3,]    9   14   19

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM