2014-01-28 54 views
0

我有200行两列的矩阵的一个子集:检索矩阵

 Col1 Col2 
    [1,] 470 535 
    [2,] 490 522 
    [3,] 482 509 
    [4,] 473 517 
    [5,] 461 524 
    [6,] 493 528 
    [7,] 498 518 
    [8,] 502 530 
    [9,] 480 520 

... 
[194,] 513 521 
[195,] 500 509 
[196,] 501 532 
[197,] 517 549 
[198,] 501 550 
[199,] 504 525 
[200,] 493 521 

我希望削减这矩阵,那我只有第10行:

 [1,] 470 535 
     [2,] 490 522 
     [3,] 482 509 
     [4,] 473 517 
     [5,] 461 524 
     [6,] 493 528 
     [7,] 498 518 
     [8,] 502 530 
     [9,] 480 520 
    [10,] 489 537 

我该怎么做r?

+1

'head(YourMatrix,10)'? – A5C1D2H2I1M1N2O1R2T1

+0

但是如果我想要有第2行到第12行,我也可以使用这种方法吗? – Kaja

回答

2

对于你的具体问题,你可以简单地使用head。对于你在评论中的问题,你可以使用基本[提取:

m <- matrix(sequence(100), ncol = 2) ## Sample data 

## `head` defaults to returning the first 6 rows... 
> head(m) 
    [,1] [,2] 
[1,] 1 51 
[2,] 2 52 
[3,] 3 53 
[4,] 4 54 
[5,] 5 55 
[6,] 6 56 

## ... but has an optional argument if you want to see more.... 
> head(m, 10) 
     [,1] [,2] 
[1,] 1 51 
[2,] 2 52 
[3,] 3 53 
[4,] 4 54 
[5,] 5 55 
[6,] 6 56 
[7,] 7 57 
[8,] 8 58 
[9,] 9 59 
[10,] 10 60 

## Use basic `[` extracting to get a specific subset 
> m[2:12, ] 
     [,1] [,2] 
[1,] 2 52 
[2,] 3 53 
[3,] 4 54 
[4,] 5 55 
[5,] 6 56 
[6,] 7 57 
[7,] 8 58 
[8,] 9 59 
[9,] 10 60 
[10,] 11 61 
[11,] 12 62