【问题标题】:creating small matrix from large matrix in r从r中的大矩阵创建小矩阵
【发布时间】:2012-07-23 19:18:50
【问题描述】:

我有 n x n 类型的矩阵,其中n = 100 左右。

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

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

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

因此生成的矩阵将像这样循环:

        I1   I20   I33   I44  I64
I1
I20
I33
I44
I64

有办法吗?

【问题讨论】:

    标签: r matrix subset


    【解决方案1】:

    直接通过[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
    

    【讨论】:

      猜你喜欢
      • 2015-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多