Inverse diagonal on the matrix

So I have 2 dataframes . depict them like this:

  data1 <- data.frame(c(1.2, 1.4, 1.3), c(1.12,1.1, 1.9), c(1.8, 1.1, 1.32)) data2 <- data.frame(c(0.4, 0.2, 0.3), c(0.1, 0.1, 0.4), c(0.5, 0.7, 0.4)) 

how to create a joint matrix that will look like this:

 combined.data > 1.2 1.12 1.80 1.4 1.10 0.7 1.3 0.4 0.4 

the upper left matrix is data1 , the lower right is data2 . I

So far, my code has been like this, and I get what I want, but up and down:

 new <- -data1 #new <- new[, rev(colnames(data1))] # diag(new) <- NA corel <- data2 #corel <- corel[, rev(colnames(data2))] new[upper.tri(new)] <- corel[upper.tri(corel)] 

And I need the rows and column order to be intact

Any thoughts?

After using Haboryme, take it, I somehow got the results. And I created a heatmap to show it. The problem is that in some spaces the data overlaps 2 matrices.

Here is the heatmap enter image description here

Above the diagonal, it should be completely red, and below the diagonal, it should be blue.

I accepted the answer to Haborime. and the problem is that my data was 32 * 31. if your matrix is not square, it will be a mess! I just added 1 dummy column to my dataset and it worked like a charm!

+7
matrix r
source share
1 answer

You can do:

 data1 <- data.frame(c(1.2, 1.4, 1.3), c(1.12,1.1, 1.9), c(1.8, 1.1, 1.32)) data2 <- data.frame(c(0.4, 0.2, 0.3), c(0.1, 0.1, 0.4), c(0.5, 0.7, 0.4)) combined=as.matrix(data1) combined[apply(lower.tri( as.matrix(data2)), 1, rev)] <-as.matrix(data2)[apply(lower.tri( as.matrix(data2)), 1, rev)] > combined c.1.2..1.4..1.3. c.1.12..1.1..1.9. c.1.8..1.1..1.32. [1,] 1.2 1.12 1.8 [2,] 1.4 1.10 0.7 [3,] 1.3 0.40 0.4 
0
source share

All Articles