2012-07-23 171 views
1

我有n×n类型的矩阵,其中n = 100左右。从大矩阵中创建小矩阵r

mat <- matrix (1:10000, nrow=100) 
rownames (mat) <- paste ("I", 1:100, sep = "") 
colnames (mat) <- paste ("I", 1:100, sep = "") 

我想选择特定的行(或列)并创建一个小的n×n矩阵。像

# for example rows selected: 
c(1, 20, 33, 44, 64). 

因此结果矩阵循环:

 I1 I20 I33 I44 I64 
I1 
I20 
I33 
I44 
I64 

有没有办法做到这一点?

回答

5

只需直接通过[row,col]

indices <- c(1, 20, 33, 44, 64) 

mat[indices,indices] 

> mat[indices,indices] 
    I1 I20 I33 I44 I64 
I1 1 1901 3201 4301 6301 
I20 20 1920 3220 4320 6320 
I33 33 1933 3233 4333 6333 
I44 44 1944 3244 4344 6344 
I64 64 1964 3264 4364 6364 
选择它们