1
votes

I would like to subset part of a large pairwise matrix into smaller matrices. e.g.

    TF1 TF2 TF3 TG1 TG2 TG3
TF1 0    2   1  450 460 450
TF2 2    0   1  452 462 462
TF3 1    2   0  451 461 451
TG1 450 452 450 0   2   0
TG2 460 462 462 2   0   1
TG3 450 452 451 1   2   0

I can subset if I input the exact column and row name "TF1" etc but I want e.g. all of the TFs.

I reckon it may be a grep but I cant put grep anywhere in my forumula:

TF <-T_PW[c("TF1","TF2","TF3"),c("TF1","TF2","TF3")]

This is the expected output matrix:

    TF2 TF3 TF1   
TF1 0   2   1
TF2 2   0   1
TF3 1   2   0

Is it because since it is a pairwise then the columns and rows have names??

Thank you

1
Can you provide the desired output? How many "smaller matrices" do you want to get from the large matrix? - acylam
Hi there, yep the desired output would be: - babyl
TF1 TF2 TF3 TF1 0 2 1 TF2 2 0 1 TF3 1 2 0 So just subsetting a part of the bigger matrix into a smaller one. - babyl

1 Answers

0
votes

grep returns the indices of the input that match the regex provided:

> grep('^TF', rownames(T_PW))
[1] 1 2 3

> grep('^TF', colnames(T_PW))
[1] 1 2 3

You can therefore place grep inside [] to subset the rownames and colnames of the matrix:

TF = T_PW[grep('^TF', rownames(T_PW)), grep('^TF', colnames(T_PW))]

Since the rownames are symmetric to the colnames, you can also do something like this:

TF_names = grep('^TF', colnames(T_PW))

T_PW[TF_names, TF_names]

Result:

    TF1 TF2 TF3
TF1   0   2   1
TF2   2   0   1
TF3   1   2   0

Data:

T_PW = read.table(text="    TF1 TF2 TF3 TG1 TG2 TG3
TF1 0    2   1  450 460 450
                 TF2 2    0   1  452 462 462
                 TF3 1    2   0  451 461 451
                 TG1 450 452 450 0   2   0
                 TG2 460 462 462 2   0   1
                 TG3 450 452 451 1   2   0", header = TRUE, row.names=1)

T_PW = as.matrix(T_PW)