Hi there, I hope to modify values in a vector or matrix in the following code: for (i in 1:9) { assign(paste("a.", i, sep = ""), 1:i) get(paste("a.", i, sep = ""))[i] <- i+50 } I get the following error message: Error in get(paste("a.", i, sep = ""))[i] <- i + 50 : target of assignment expands to non-language object I have read the FAQ "How can I turn a string into a variable?", however, I don't find a way to deal with: get(paste("a.", i, sep = ""))[i] <- i+50 Any suggestions? Thanks in advance! Regards, Jinsong
On Dec 11, 2011, at 10:27 AM, Jinsong Zhao wrote:> Hi there, > > I hope to modify values in a vector or matrix in the following code: > > for (i in 1:9) { > assign(paste("a.", i, sep = ""), 1:i) > get(paste("a.", i, sep = ""))[i] <- i+50 > }Just one matrix? Then you seem to have inappropriately borrowed using "." as an indexing operation. In R that is just another character when used as an object name. "a.1" is notgoing to evaulate to a[1]. Look at what you would have had after > for (i in 1:9) { + assign(paste("a.", i, sep = ""), 1:i) + } > ls() [1] "a" "a.1" "a.2" [4] "a.3" "a.4" "a.5" [7] "a.6" "a.7" "a.8" [10] "a.9" > a.1 [1] 1 > a.2 [1] 1 2 Each of those assign() operations created a single vector of length i. I doubt that was what you intended, Better would be to describe your objects and your intentions, rather than expecting us to understand your goals by just looking at code that doesn't achieve thos goals. (There is no `get<-` function which was the source of the error.)> > I get the following error message: > > Error in get(paste("a.", i, sep = ""))[i] <- i + 50 : > target of assignment expands to non-language object > > I have read the FAQ "How can I turn a string into a variable?", > however, I don't find a way to deal with: > > get(paste("a.", i, sep = ""))[i] <- i+50 > > Any suggestions? Thanks in advance! > > Regards, > Jinsong > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
You are basically in R Inferno Circle 8.1.40. http://www.burns-stat.com/pages/Tutor/R_inferno.pdf On 11/12/2011 15:27, Jinsong Zhao wrote:> Hi there, > > I hope to modify values in a vector or matrix in the following code: > > for (i in 1:9) { > assign(paste("a.", i, sep = ""), 1:i) > get(paste("a.", i, sep = ""))[i] <- i+50 > } > > I get the following error message: > > Error in get(paste("a.", i, sep = ""))[i] <- i + 50 : > target of assignment expands to non-language object > > I have read the FAQ "How can I turn a string into a variable?", however, > I don't find a way to deal with: > > get(paste("a.", i, sep = ""))[i] <- i+50 > > Any suggestions? Thanks in advance! > > Regards, > Jinsong > > ______________________________________________ > 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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
I find that get() and assign() are awkward to use and that the syntax is easier if you put your objects into a list or environment. To me, it also makes it clearer what the code is doing and keeps the output of objects() shorter and easier to manage. E.g., nResults <- 9 results <- vector("list", nResults) # or results <- new.env() for(i in 1:nResults) { resultName <- paste("a.", i, sep="") results[[resultName]] <- 1:i results[[resultName]][i] <- i+50 } 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 Jinsong Zhao > Sent: Sunday, December 11, 2011 7:28 AM > To: r-help at r-project.org > Subject: [R] how to assign a value? > > Hi there, > > I hope to modify values in a vector or matrix in the following code: > > for (i in 1:9) { > assign(paste("a.", i, sep = ""), 1:i) > get(paste("a.", i, sep = ""))[i] <- i+50 > } > > I get the following error message: > > Error in get(paste("a.", i, sep = ""))[i] <- i + 50 : > target of assignment expands to non-language object > > I have read the FAQ "How can I turn a string into a variable?", however, > I don't find a way to deal with: > > get(paste("a.", i, sep = ""))[i] <- i+50 > > Any suggestions? Thanks in advance! > > Regards, > Jinsong > > ______________________________________________ > 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.