cliff@research.telcordia.com
2004-Jul-27 21:47 UTC
[Rd] dynamic creation of matrix objects (PR#7136)
I am trying to assign the values in a matrix to an object whose name is created dynamically, i.e., using eval-parse-text-paste sequence. It seems that only the very last value in the matrix is assigned to the new object. I have included an example below where I create a matrix object called "aMat". I can assign the contents of aMat to another object called "thisMat." However, when I attempt to make a similar assignment to a new object called "thatMat", only the last value is returned, when queried. Does anybody know whether this is a bug...or am I doing something wrong? Cliff Behrens > aMat<- matrix(c(1,2,3,4,5,6,7,8),ncol=2) > aMat [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 > thisMat<- aMat > thisMat [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 7 [4,] 4 8 > tName<-"that" > eval(parse(text=paste(tName,"Mat <-",aMat,sep=""))) > thatMat [1] 8 >
How is this a bug? You get exactly what you _should_ get (or otherwise it might have been a legitimate bug report). I suspect what you really should use is assign(). Andy> From: cliff@research.telcordia.com > > I am trying to assign the values in a matrix to an object > whose name is > created dynamically, i.e., using eval-parse-text-paste sequence. It > seems that only the very last value in the matrix is assigned > to the new > object. I have included an example below where I create a > matrix object > called "aMat". I can assign the contents of aMat to another object > called "thisMat." However, when I attempt to make a similar > assignment > to a new object called "thatMat", only the last value is > returned, when > queried. Does anybody know whether this is a bug...or am I doing > something wrong? > > Cliff Behrens > > > aMat<- matrix(c(1,2,3,4,5,6,7,8),ncol=2) > > aMat > [,1] [,2] > [1,] 1 5 > [2,] 2 6 > [3,] 3 7 > [4,] 4 8 > > thisMat<- aMat > > thisMat > [,1] [,2] > [1,] 1 5 > [2,] 2 6 > [3,] 3 7 > [4,] 4 8 > > tName<-"that" > > eval(parse(text=paste(tName,"Mat <-",aMat,sep=""))) > > thatMat > [1] 8 > > > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-devel > >
Thanks to all of you for your suggestion to use "assign" instead. Cliff Behrens Pedro Pereira Rodrigues wrote:>You should consider using > > > >>eval(parse(text=paste(tName,"Mat <-aMat",sep=""))) >> >> > >instead. > >If you try only > > > >>parse(text=paste(tName,"Mat <-",aMat,sep="")) >> >> > >you see where the problem is: > >expression(thatMat <- 1, thatMat <- 2, thatMat <- 3, thatMat <- 4, > thatMat <- 5, thatMat <- 6, thatMat <- 7, thatMat <- 8) > > >Good Luck. > > >On Tue, 2004-07-27 at 20:47, cliff@research.telcordia.com wrote: > > >>I am trying to assign the values in a matrix to an object whose name is >>created dynamically, i.e., using eval-parse-text-paste sequence. It >>seems that only the very last value in the matrix is assigned to the new >>object. I have included an example below where I create a matrix object >>called "aMat". I can assign the contents of aMat to another object >>called "thisMat." However, when I attempt to make a similar assignment >>to a new object called "thatMat", only the last value is returned, when >>queried. Does anybody know whether this is a bug...or am I doing >>something wrong? >> >>Cliff Behrens >> >> > aMat<- matrix(c(1,2,3,4,5,6,7,8),ncol=2) >> > aMat >> [,1] [,2] >>[1,] 1 5 >>[2,] 2 6 >>[3,] 3 7 >>[4,] 4 8 >> > thisMat<- aMat >> > thisMat >> [,1] [,2] >>[1,] 1 5 >>[2,] 2 6 >>[3,] 3 7 >>[4,] 4 8 >> > tName<-"that" >> > eval(parse(text=paste(tName,"Mat <-",aMat,sep=""))) >> > thatMat >>[1] 8 >> > >> >>______________________________________________ >>R-devel@stat.math.ethz.ch mailing list >>https://www.stat.math.ethz.ch/mailman/listinfo/r-devel >> >>[[alternative HTML version deleted]]
Thanks to all of you for your suggestion to use "assign" instead. Cliff Behrens Liaw, Andy wrote:>How is this a bug? You get exactly what you _should_ get (or otherwise it >might have been a legitimate bug report). > >I suspect what you really should use is assign(). > >Andy > > > >>From: cliff@research.telcordia.com >> >>I am trying to assign the values in a matrix to an object >>whose name is >>created dynamically, i.e., using eval-parse-text-paste sequence. It >>seems that only the very last value in the matrix is assigned >>to the new >>object. I have included an example below where I create a >>matrix object >>called "aMat". I can assign the contents of aMat to another object >>called "thisMat." However, when I attempt to make a similar >>assignment >>to a new object called "thatMat", only the last value is >>returned, when >>queried. Does anybody know whether this is a bug...or am I doing >>something wrong? >> >>Cliff Behrens >> >> > aMat<- matrix(c(1,2,3,4,5,6,7,8),ncol=2) >> > aMat >> [,1] [,2] >>[1,] 1 5 >>[2,] 2 6 >>[3,] 3 7 >>[4,] 4 8 >> > thisMat<- aMat >> > thisMat >> [,1] [,2] >>[1,] 1 5 >>[2,] 2 6 >>[3,] 3 7 >>[4,] 4 8 >> > tName<-"that" >> > eval(parse(text=paste(tName,"Mat <-",aMat,sep=""))) >> > thatMat >>[1] 8 >> > >> >>______________________________________________ >>R-devel@stat.math.ethz.ch mailing list >>https://www.stat.math.ethz.ch/mailman/listinfo/r-devel >> >> >> >> > > >------------------------------------------------------------------------------ >Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (One Merck Drive, Whitehouse Station, New Jersey, USA 08889), and/or its affiliates (which may be known outside the United States as Merck Frosst, Merck Sharp & Dohme or MSD and in Japan, as Banyu) that may be confidential, proprietary copyrighted and/or legally privileged. It is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please notify us immediately by reply e-mail and then delete it from your system. >------------------------------------------------------------------------------ > >[[alternative HTML version deleted]]
PLEASE read the FAQ about BUGS. R-bugs is not for users to report their own lack of understanding, and not for questions. Take a look at> parse(text=paste(tName,"Mat <-",aMat,sep=""))expression(thatMat <- 1, thatMat <- 2, thatMat <- 3, thatMat <- 4, thatMat <- 5, thatMat <- 6, thatMat <- 7, thatMat <- 8) The bug is in your understanding of your own code, which is working correctly. What do you really want to do here? assign(paste(tName,"Mat", sep=""), aMat) perhaps? On Tue, 27 Jul 2004 cliff@research.telcordia.com wrote:> I am trying to assign the values in a matrix to an object whose name is > created dynamically, i.e., using eval-parse-text-paste sequence. It > seems that only the very last value in the matrix is assigned to the new > object. I have included an example below where I create a matrix object > called "aMat". I can assign the contents of aMat to another object > called "thisMat." However, when I attempt to make a similar assignment > to a new object called "thatMat", only the last value is returned, when > queried. Does anybody know whether this is a bug...or am I doing > something wrong? > > Cliff Behrens > > > aMat<- matrix(c(1,2,3,4,5,6,7,8),ncol=2) > > aMat > [,1] [,2] > [1,] 1 5 > [2,] 2 6 > [3,] 3 7 > [4,] 4 8 > > thisMat<- aMat > > thisMat > [,1] [,2] > [1,] 1 5 > [2,] 2 6 > [3,] 3 7 > [4,] 4 8 > > tName<-"that" > > eval(parse(text=paste(tName,"Mat <-",aMat,sep=""))) > > thatMat > [1] 8 > > > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-devel > >-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595