How can I extract a list of the positions in the matrix?> A=matrix(1:8, nrow=2,ncol=4) > A[,1] [,2] [,3] [,4] [1,] 1 3 5 7 [2,] 2 4 6 8 Something like this pos.A 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4
Here's one way:> data.frame(rowID=as.vector(row(A)), colID=as.vector(col(A)), A=as.vector(A))rowID colID A 1 1 1 1 2 2 1 2 3 1 2 3 4 2 2 4 5 1 3 5 6 2 3 6 7 1 4 7 8 2 4 8 You can sort that as desired. Sarah On Sun, Jan 1, 2012 at 5:21 PM, Ana <rrasterr at gmail.com> wrote:> How can I extract a list of the positions in the matrix? >> A=matrix(1:8, nrow=2,ncol=4) >> A > ? ? [,1] [,2] [,3] [,4] > [1,] ? ?1 ? ?3 ? ?5 ? ?7 > [2,] ? ?2 ? ?4 ? ?6 ? ?8 > > > Something like this > > pos.A > > 1 1 > 1 2 > 1 3 > 1 4 > 2 1 > 2 2 > 2 3 > 2 4 >-- Sarah Goslee http://www.functionaldiversity.org
Hi Ana, most probably this is one of the more ugly solutions:> d=dim(A) > d[1] 2 4> cbind(rep(1:d[1], each=d[2]), rep(1:d[2], d[1]))[,1] [,2] [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 [5,] 2 1 [6,] 2 2 [7,] 2 3 [8,] 2 4 Thanks, wr * Ana <rrasterr at gmail.com> [2012-01-01 23:21:12 +0100]:> How can I extract a list of the positions in the matrix? > > A=matrix(1:8, nrow=2,ncol=4) > > A > [,1] [,2] [,3] [,4] > [1,] 1 3 5 7 > [2,] 2 4 6 8 > > > Something like this > > pos.A > > 1 1 > 1 2 > 1 3 > 1 4 > 2 1 > 2 2 > 2 3 > 2 4 > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi Ana, most probably this is one of the more ugly solutions:> d=dim(A) > d[1] 2 4> cbind(rep(1:d[1], each=d[2]), rep(1:d[2], d[1]))[,1] [,2] [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 [5,] 2 1 [6,] 2 2 [7,] 2 3 [8,] 2 4 Thanks, wr * Ana <rrasterr at gmail.com> [2012-01-01 23:21:12 +0100]:> How can I extract a list of the positions in the matrix? > > A=matrix(1:8, nrow=2,ncol=4) > > A > [,1] [,2] [,3] [,4] > [1,] 1 3 5 7 > [2,] 2 4 6 8 > > > Something like this > > pos.A > > 1 1 > 1 2 > 1 3 > 1 4 > 2 1 > 2 2 > 2 3 > 2 4 > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Hi Ana,> d=dim(A) > d[1] 2 4> cbind(rep(1:d[1], each=d[2]), rep(1:d[2], d[1]))[,1] [,2] [1,] 1 1 [2,] 1 2 [3,] 1 3 [4,] 1 4 [5,] 2 1 [6,] 2 2 [7,] 2 3 [8,] 2 4 Thanks, wr * Ana <rrasterr at gmail.com> [2012-01-01 23:21:12 +0100]:> How can I extract a list of the positions in the matrix? > > A=matrix(1:8, nrow=2,ncol=4) > > A > [,1] [,2] [,3] [,4] > [1,] 1 3 5 7 > [2,] 2 4 6 8 > > > Something like this > > pos.A > > 1 1 > 1 2 > 1 3 > 1 4 > 2 1 > 2 2 > 2 3 > 2 4 > > ______________________________________________ > R-help at r-project.org mailing list > 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.