Hi all, I have a large square matrix (60 x 60) and found it hard to visualize. Is it possible to change it as shown below? Sample example (3 x 3) A B C A 3 4 5 B 4 7 8 C 5 8 9 Desired output A A 3 A B 4 A C 5 B B 7 B C 8 C C 9 Thank you in advance
On 16/07/2016 6:25 PM, Ashta wrote: > Hi all, > > I have a large square matrix (60 x 60) and found it hard to > visualize. Is it possible to change it as shown below? > > Sample example (3 x 3) > > A B C > A 3 4 5 > B 4 7 8 > C 5 8 9 > > Desired output > A A 3 > A B 4 > A C 5 > B B 7 > B C 8 > C C 9 Yes, use matrix indexing. I don't think the 3600 values are going to be very easy to read, but here's how to produce them: m <- matrix(1:3600, 60, 60) indices <- expand.grid(row = 1:60, col = 1:60) cbind(indices$row, indices$col, m[as.matrix(indices)]) Duncan Murdoch
I'm not sure what the OP is looking for in the first two columns, but he does seem to be looking for only the diagonal and super-diagonal elements. Here's some code that makes output that looks similar to the "Desired output": mat1 <- matrix(rbind(c(3, 4, 5), c(4, 7, 8), c(5, 8, 9)), nrow=3) colnames(mat1) <- c('A', 'B', 'C') rownames(mat1) <- c('A', 'B', 'C') mat1 col3 <- mat1[row(mat1) >= col(mat1)] idx12 <- which(row(mat1) >= col(mat1), arr.ind=TRUE) desiredOutput <- cbind(idx12[ , 2], idx12[ , 1], col3) desiredOutput #### If you really want 'A', 'B', 'C' col1 <- colnames(mat1)[idx12[ , 2]] col1 col2 <- rownames(mat1)[idx12[ , 1]] col2 desiredOutput <- cbind(col1, col2, col3) desiredOutput -- Mike On Sat, Jul 16, 2016 at 4:39 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> On 16/07/2016 6:25 PM, Ashta wrote: >> Hi all, >> >> I have a large square matrix (60 x 60) and found it hard to >> visualize. Is it possible to change it as shown below? >> >> Sample example (3 x 3) >> >> A B C >> A 3 4 5 >> B 4 7 8 >> C 5 8 9 >> >> Desired output >> A A 3 >> A B 4 >> A C 5 >> B B 7 >> B C 8 >> C C 9 > > Yes, use matrix indexing. I don't think the 3600 values are going to be > very easy to read, but here's how to produce them: > > m <- matrix(1:3600, 60, 60) > indices <- expand.grid(row = 1:60, col = 1:60) > cbind(indices$row, indices$col, m[as.matrix(indices)]) > > Duncan Murdoch > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On 07/17/2016 01:39 AM, Duncan Murdoch wrote:> On 16/07/2016 6:25 PM, Ashta wrote: > > Hi all, > > > > I have a large square matrix (60 x 60) and found it hard to > > visualize. Is it possible to change it as shown below? > > > > Sample example (3 x 3) > > > > A B C > > A 3 4 5 > > B 4 7 8 > > C 5 8 9 > > > > Desired output > > A A 3 > > A B 4 > > A C 5 > > B B 7 > > B C 8 > > C C 9 > > Yes, use matrix indexing. I don't think the 3600 values are going to be > very easy to read, but here's how to produce them: > > m <- matrix(1:3600, 60, 60) > indices <- expand.grid(row = 1:60, col = 1:60) > cbind(indices$row, indices$col, m[as.matrix(indices)]) >Or use as.data.frame.table(): m <- matrix(1:9, 3, 3, dimnames = list(dimA = letters[1:3], dimB = letters[1:3])) m as.data.frame.table(m, responseName = "value") --- I do not know what you mean by "visualize", but image() or heatmap() are good starting points if you need a plot of the values. If you really need to inspect the raw values, you can try interactive (scrollable) tables, e.g.: library(DT) m <- provideDimnames(matrix(1:3600, 60, 60)) datatable(m, options = list(pageLength = 60)) Cheers, Denes> Duncan Murdoch > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.