gm007
gm007

Reputation: 587

Match row names and column names of one matrix to another to do element by element calculations

I have two matrices and I need to do a row name and column name match to conduct element by element calculations. The first calculation is phij/pij and the second is ((phij-pij)^2)/pij The long matrix phij has row names separated by a dash e.g. Aaa-Baa. The column names have no dash. I need to match the part of the row name after the dash i.e. Baa and a column name in the phij matrix to the row name and column name of the smaller matrix pij.

I tried using a for loop but it's not matching the actual row names and column names but instead looks up positions in the sequence.

LR<-phij
ChiSq<-phij

ROWS <- data.frame(ROW0=rownames(phij),
                     ROW1=substr(rownames(phij),regexpr("-", rownames(phij))+1,nchar(rownames(phij))))
  
  COLNAMES  <- c(colnames(phij))
  for(rowN in 1:length(ROWS$ROW0)){
    for(colN in COLNAMES){
      LR[ROWS$ROW0[rowN],colN]<-LR[ROWS$ROW0[rowN],colN]/pij[ROWS$ROW1[rowN],colN]
      ChiSq[ROWS$ROW0[rowN],colN]<-((ChiSq[ROWS$ROW0[rowN],colN]-pij[ROWS$ROW1[rowN],colN])^2)/pij[ROWS$ROW1[rowN],colN]
    }
  }

Data:

phij:

structure(c(0.111111111111111, 0.2, 0, 0, 0, 0.25, 0, 0.666666666666667, 
0.166666666666667, 0, 0, 0.666666666666667, 0.5, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.4, 0.333333333333333, 0, 1, 
0, 0.166666666666667, 0, 0.571428571428571, 0, 0, 0, 0.4, 0.272727272727273, 
0, 0, 0, 0, 0, 0, 0, 0, 0.222222222222222, 0.6, 0, 0, 0.25, 0, 
0, 0.333333333333333, 0.333333333333333, 0, 0, 0.333333333333333, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.5, 1, 0, 0, 0, 0, 0, 0.2, 0.666666666666667, 
0, 0, 0, 0.166666666666667, 0, 0.142857142857143, 1, 0, 0, 0.2, 
0.272727272727273, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.222222222222222, 0.2, 0, 0, 0, 0, 
0, 0, 0.166666666666667, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0.2, 0, 0, 0, 0, 0.333333333333333, 0, 0.142857142857143, 
0, 0, 0, 0.2, 0.181818181818182, 0, 0, 0, 0, 0, 0, 0, 0, 0.444444444444444, 
0, 0, 0, 0.75, 0.5, 0, 0, 0.333333333333333, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.5, 0, 0, 0, 1, 1, 0, 0.2, 0, 0, 0, 1, 0.333333333333333, 
0, 0.142857142857143, 0, 0, 1, 0, 0.272727272727273, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), class = "table", .Dim = c(49L, 
7L), .Dimnames = list(i = c("A-A", "A-Aa", "A-Aaa", "A-B", "A-Ba", 
"A-Baa", "A-Caa", "Aa-A", "Aa-Aa", "Aa-Aaa", "Aa-B", "Aa-Ba", 
"Aa-Baa", "Aa-Caa", "Aaa-A", "Aaa-Aa", "Aaa-Aaa", "Aaa-B", "Aaa-Ba", 
"Aaa-Baa", "Aaa-Caa", "B-A", "B-Aa", "B-Aaa", "B-B", "B-Ba", 
"B-Baa", "B-Caa", "Ba-A", "Ba-Aa", "Ba-Aaa", "Ba-B", "Ba-Ba", 
"Ba-Baa", "Ba-Caa", "Baa-A", "Baa-Aa", "Baa-Aaa", "Baa-B", "Baa-Ba", 
"Baa-Baa", "Baa-Caa", "Caa-A", "Caa-Aa", "Caa-Aaa", "Caa-B", 
"Caa-Ba", "Caa-Baa", "Caa-Caa"), j = c("A", "Aa", "Aaa", "B", 
"Ba", "Baa", "Caa")))

pij:

structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0.608695652173913, 0.323529411764706, 
0.129032258064516, 0.176470588235294, 0.125, 0, 0, 0.173913043478261, 
0.323529411764706, 0.258064516129032, 0.294117647058824, 0.25, 
0, 0, 0.0869565217391304, 0.235294117647059, 0.419354838709677, 
0.352941176470588, 0.25, 0, 0, 0.130434782608696, 0.117647058823529, 
0.161290322580645, 0.117647058823529, 0.25, 0, 0, 0, 0, 0.032258064516129, 
0.0588235294117647, 0.125, 0, 0, 0, 0, 0, 0, 0, 0), class = "table", .Dim = c(7L, 
7L), .Dimnames = list(i = c("Aaa", "Aa", "A", "Baa", "Ba", "B", 
"Caa"), j = c("Aaa", "Aa", "A", "Baa", "Ba", "B", "Caa")))

Upvotes: 0

Views: 70

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388862

You can create a new matrix of pij which is of same dimension as phij and then perform the calculations that you want.

new_pij <- pij[sub('-.*', '', rownames(phij)), colnames(phij)]

You can then do :

phij/new_pij

and

((phij-new_pij)^2)/new_pij

Upvotes: 1

Related Questions