Another way: > A <- matrix(1:9,3,3, dimnames=list(Row=paste0("r",1:3),Col=paste0("c",1:3))) > A Col Row c1 c2 c3 r1 1 4 7 r2 2 5 8 r3 3 6 9 > matrix( A[row(A)!=col(A)], nrow(A)-1, ncol(A), dimnames=list(NULL, colnames(A))) c1 c2 c3 [1,] 2 4 7 [2,] 3 6 8
Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Nov 14, 2018 at 2:32 PM, Richard M. Heiberger <r...@temple.edu> wrote: > An even better solution because it has fewer steps. > > A <- matrix(1:9, 3, 3) > A > B <- A[-1, ] > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > B > > > A <- matrix(1:9, 3, 3) > > A > [,1] [,2] [,3] > [1,] 1 4 7 > [2,] 2 5 8 > [3,] 3 6 9 > > B <- A[-1, ] > > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > > B > [,1] [,2] [,3] > [1,] 2 4 7 > [2,] 3 6 8 > > > On Wed, Nov 14, 2018 at 2:09 PM Richard M. Heiberger <r...@temple.edu> > wrote: > > > > Steve's method is very slick. > > > > I think this is a bit easier to understand. > > > > A <- matrix(1:9, 3, 3) > > A > > B <- matrix(nrow=2, ncol=3) > > B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] > > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > > B > > > > > A <- matrix(1:9, 3, 3) > > > A > > [,1] [,2] [,3] > > [1,] 1 4 7 > > [2,] 2 5 8 > > [3,] 3 6 9 > > > B <- matrix(nrow=2, ncol=3) > > > B[lower.tri(B, diag=TRUE)] <- A[lower.tri(A)] > > > B[upper.tri(B, diag=FALSE)] <- A[upper.tri(A)] > > > B > > [,1] [,2] [,3] > > [1,] 2 4 7 > > [2,] 3 6 8 > > > > > On Wed, Nov 14, 2018 at 11:04 AM S Ellison <s.elli...@lgcgroup.com> > wrote: > > > > > > i) Your code creates w2 but references w1 to create aa. > > > > > > So you needed > > > aa <- matrix(rep(c(0.4, 0.1, 0.2), 3), 3,3) > > > for a working example. > > > > > > ii) This > > > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > > removes any value that is present in the diagonal of aa. Look up > ?"%in%" to see what that does; it returns TRUE whenever anything in > as.numeric(aa) matches anything in your diagonal. All the values in aa > match one of c(0.4, 0.1, 0.2). So since your whole matrix consists of these > three numbers, you told R to leave out everything in aa and then create a > 2x3 matrix with the result. Hence the NAs > > > > > > iii) If you want to extract odd parts of a matrix explicitly, see ?"[" > and particularly the section on indexing using arrays > > > > > > iv) You can use logical indexing. In the special case of the diagonal, > you can use diag() to create a matrix of logicals, logically negate that > and apply that to your matrix: > > > aa[ !diag(rep(TRUE, 3)) ] > > > > > > and, in twoi rows: > > > matrix( aa[ !diag(rep(TRUE, 3)) ], 2,3) > > > > > > > for examplei have this matrix > > > > w2<-c(0.1,0.2,0.4,0.2,0.4,0.1) > > > > aa<-matrix(w1,nrow=3,ncol=3) > > > > aa > > > > [,1] [,2] [,3] > > > > [1,] 0.4 0.4 0.4 > > > > [2,] 0.1 0.1 0.1 > > > > [3,] 0.2 0.2 0.2 > > > > > > > > if i use this code > > > > matrix(as.numeric(aa)[!as.numeric(aa) %in% diag(aa)],2,3) > > > > > > > > i will obtaine this matrix[,1] [,2] [,3] > > > > [1,] NA NA NA > > > > [2,] NA NA NA > > > > > > > > but me i want this matrix[,1] [,2] [,3] > > > > [1,] 0.1 0.4 0.4 > > > > [2,] 0.2 0.2 0.1 > > > > > > > > thank you > > > > > > > > [[alternative HTML version deleted]] > > > > > > > > ______________________________________________ > > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > > PLEASE do read the posting guide http://www.R-project.org/posting- > > > > guide.html > > > > and provide commented, minimal, self-contained, reproducible code. > > > > > > > > > ******************************************************************* > > > This email and any attachments are confidential. Any use, copying or > > > disclosure other than by the intended recipient is unauthorised. If > > > you have received this message in error, please notify the sender > > > immediately via +44(0)20 8943 7000 or notify postmas...@lgcgroup.com > > > and delete this message and any copies from your computer and network. > > > LGC Limited. Registered in England 2991879. > > > Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK > > > ______________________________________________ > > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > > https://stat.ethz.ch/mailman/listinfo/r-help > > > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > > > and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.