fprd
fprd

Reputation: 621

creating small matrix from large matrix in r

I have n x n type of matrix where n = 100 or so.

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

I want to select specific rows (or columns) and create a small n x n matrix.

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

Thus resulting matrix will loop like:

        I1   I20   I33   I44  I64
I1
I20
I33
I44
I64

Is there way to do so ?

Upvotes: 1

Views: 2212

Answers (1)

mindless.panda
mindless.panda

Reputation: 4092

Just select them directly via [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

Upvotes: 5

Related Questions