Hi, I would like to have an index for a column in a matrix encoded in a cell of the same matrix. For example: x = matrix(c(11,12,13,1, 21,22,23,3, 31,32,33,2),byrow=T,ncol=4) In this case, column 4 is the index. I then access the column specified in the index by:> for (i in 1:3) print(x[i,x[i,4]])[1] 11 [1] 23 [1] 32> > for (i in 1:3) {x[i,x[i,4]] <- x[i,x[i,4]] + 5} > x[,1] [,2] [,3] [,4] [1,] 16 12 13 1 [2,] 21 22 28 3 [3,] 31 37 33 2>Is there a way to get the same results without looping? Best, Iuri.
> diag(x[,x[,4]])[1] 11 23 32> diag(x[,x[,4]]) <- diag(x[,x[,4]])+5 > x[,1] [,2] [,3] [,4] [1,] 16 12 13 1 [2,] 21 22 28 3 [3,] 31 37 33 2 Cheers Joris On Fri, Jun 18, 2010 at 8:58 PM, Iuri Gavronski <iuri at ufrgs.br> wrote:> Hi, > > I would like to have an index for a column in a matrix encoded in a > cell of the same matrix. > For example: > x = matrix(c(11,12,13,1, > 21,22,23,3, > 31,32,33,2),byrow=T,ncol=4) > > In this case, column 4 is the index. I then access the column > specified in the index by: >> for (i in 1:3) print(x[i,x[i,4]]) > [1] 11 > [1] 23 > [1] 32 >> >> for (i in 1:3) {x[i,x[i,4]] <- x[i,x[i,4]] + 5} >> x > ? ? [,1] [,2] [,3] [,4] > [1,] ? 16 ? 12 ? 13 ? ?1 > [2,] ? 21 ? 22 ? 28 ? ?3 > [3,] ? 31 ? 37 ? 33 ? ?2 >> > > Is there a way to get the same results without looping? > > Best, > > Iuri. > > ______________________________________________ > 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. >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
On second thought, the addition only works in the case of a square matrix : x = matrix(c(11,12,13,1, 21,22,23,3, 31,32,33,2, 41,42,43,1, 51,52,53,3),byrow=T,ncol=4)> diag(x[,x[,4]])[1] 11 23 32 41 53 works, but> diag(x[,x[,4]]) <- diag(x[,x[,4]])+5 > x[,1] [,2] [,3] [,4] [1,] 11 12 13 1 [2,] 21 22 23 3 [3,] 31 37 33 2 [4,] 46 42 43 1 [5,] 51 52 58 3 only changes the last three rows. Quite puzzling in fact... Cheers Joris On Sat, Jun 19, 2010 at 2:07 AM, Joris Meys <jorismeys at gmail.com> wrote:>> diag(x[,x[,4]]) > [1] 11 23 32 > >> diag(x[,x[,4]]) <- diag(x[,x[,4]])+5 >> x > ? ? [,1] [,2] [,3] [,4] > [1,] ? 16 ? 12 ? 13 ? ?1 > [2,] ? 21 ? 22 ? 28 ? ?3 > [3,] ? 31 ? 37 ? 33 ? ?2 > > Cheers > Joris > > On Fri, Jun 18, 2010 at 8:58 PM, Iuri Gavronski <iuri at ufrgs.br> wrote: >> Hi, >> >> I would like to have an index for a column in a matrix encoded in a >> cell of the same matrix. >> For example: >> x = matrix(c(11,12,13,1, >> 21,22,23,3, >> 31,32,33,2),byrow=T,ncol=4) >> >> In this case, column 4 is the index. I then access the column >> specified in the index by: >>> for (i in 1:3) print(x[i,x[i,4]]) >> [1] 11 >> [1] 23 >> [1] 32 >>> >>> for (i in 1:3) {x[i,x[i,4]] <- x[i,x[i,4]] + 5} >>> x >> ? ? [,1] [,2] [,3] [,4] >> [1,] ? 16 ? 12 ? 13 ? ?1 >> [2,] ? 21 ? 22 ? 28 ? ?3 >> [3,] ? 31 ? 37 ? 33 ? ?2 >>> >> >> Is there a way to get the same results without looping? >> >> Best, >> >> Iuri. >> >> ______________________________________________ >> 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. >> > > > > -- > Joris Meys > Statistical consultant > > Ghent University > Faculty of Bioscience Engineering > Department of Applied mathematics, biometrics and process control > > tel : +32 9 264 59 87 > Joris.Meys at Ugent.be > ------------------------------- > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
Does the following do what you want? > i <- cbind(seq_len(nrow(x)), x[,4]) > x[i] [1] 11 23 32 > x[i] <- x[i] + 5 > x [,1] [,2] [,3] [,4] [1,] 16 12 13 1 [2,] 21 22 28 3 [3,] 31 37 33 2 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Joris Meys > Sent: Friday, June 18, 2010 5:08 PM > To: Iuri Gavronski > Cc: r-help > Subject: Re: [R] Non-procedural access to columns of a matrix > > > diag(x[,x[,4]]) > [1] 11 23 32 > > > diag(x[,x[,4]]) <- diag(x[,x[,4]])+5 > > x > [,1] [,2] [,3] [,4] > [1,] 16 12 13 1 > [2,] 21 22 28 3 > [3,] 31 37 33 2 > > Cheers > Joris > > On Fri, Jun 18, 2010 at 8:58 PM, Iuri Gavronski <iuri at ufrgs.br> wrote: > > Hi, > > > > I would like to have an index for a column in a matrix encoded in a > > cell of the same matrix. > > For example: > > x = matrix(c(11,12,13,1, > > 21,22,23,3, > > 31,32,33,2),byrow=T,ncol=4) > > > > In this case, column 4 is the index. I then access the column > > specified in the index by: > >> for (i in 1:3) print(x[i,x[i,4]]) > > [1] 11 > > [1] 23 > > [1] 32 > >> > >> for (i in 1:3) {x[i,x[i,4]] <- x[i,x[i,4]] + 5} > >> x > > ? ? [,1] [,2] [,3] [,4] > > [1,] ? 16 ? 12 ? 13 ? ?1 > > [2,] ? 21 ? 22 ? 28 ? ?3 > > [3,] ? 31 ? 37 ? 33 ? ?2 > >> > > > > Is there a way to get the same results without looping? > > > > Best, > > > > Iuri. > > > > ______________________________________________ > > 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. > > > > > > -- > Joris Meys > Statistical consultant > > Ghent University > Faculty of Bioscience Engineering > Department of Applied mathematics, biometrics and process control > > tel : +32 9 264 59 87 > Joris.Meys at Ugent.be > ------------------------------- > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php > > ______________________________________________ > 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. >