Dear all, I am trying to manually re-sort rows in a number of tables. The rows aren't sorted on any particular values but are simply ordered by user choice (as shown by the row numbers in the code). I have been able to carry out each re-arrangement without the use of the 'for' loop, but cannot seem to successfully execute the statements when incorporated into the loop. The code I have is as follows: table_year=1951 for (i in (paste("arunoff_",year,"_temp",sep=""))) { assign(paste("arunoff_",table_year, sep=""),paste("arunoff_",table_year,"_temp")[c(10,7,9,5,4,12,1,3,2,8,11,6),]) table_year = table_year+1 } The error I get is: Error in paste("arunoff_", table_year, "_temp")[c(10, 7, 9, 5, 4, 12, : incorrect number of dimensions ...depsite this not occurring when I do each table individually (so it can't be a case of there not being enough rows, as> dim(arunoff_1951_temp) gives [1] 12 11 I have a feeling that it may be a syntax error, possibly between 'temp' and the square bracket, but I can't be sure of this. Any solutions or advice offered would be gratefully received. Many thanks, Steve _________________________________________________________________ [[elided Hotmail spam]]
Rowe, Brian Lee Yung (Portfolio Analytics)
2009-Mar-25 18:20 UTC
[R] Manual sort in a for loop
Aren't you missing a sep='' in your last call to paste? -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Steve Murray Sent: Wednesday, March 25, 2009 1:58 PM To: r-help at r-project.org Subject: [R] Manual sort in a for loop Dear all, I am trying to manually re-sort rows in a number of tables. The rows aren't sorted on any particular values but are simply ordered by user choice (as shown by the row numbers in the code). I have been able to carry out each re-arrangement without the use of the 'for' loop, but cannot seem to successfully execute the statements when incorporated into the loop. The code I have is as follows: table_year=1951 for (i in (paste("arunoff_",year,"_temp",sep=""))) { assign(paste("arunoff_",table_year, sep=""),paste("arunoff_",table_year,"_temp")[c(10,7,9,5,4,12,1,3,2,8,11, 6),]) table_year = table_year+1 } The error I get is: Error in paste("arunoff_", table_year, "_temp")[c(10, 7, 9, 5, 4, 12, : incorrect number of dimensions ...depsite this not occurring when I do each table individually (so it can't be a case of there not being enough rows, as> dim(arunoff_1951_temp) gives [1] 12 11 I have a feeling that it may be a syntax error, possibly between 'temp' and the square bracket, but I can't be sure of this. Any solutions or advice offered would be gratefully received. Many thanks, Steve _________________________________________________________________ [[elided Hotmail spam]] ______________________________________________ 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. -------------------------------------------------------------------------- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. References to "Merrill Lynch" are references to any company in the Merrill Lynch & Co., Inc. group of companies, which are wholly-owned by Bank of America Corporation. Securities and Insurance Products: * Are Not FDIC Insured * Are Not Bank Guaranteed * May Lose Value * Are Not a Bank Deposit * Are Not a Condition to Any Banking Service or Activity * Are Not Insured by Any Federal Government Agency. Attachments that are part of this E-communication may have additional important disclosures and disclaimers, which you should read. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. --------------------------------------------------------------------------
I assume you need to use 'get' to retrieve the value: table_year=1951 for (i in (paste("arunoff_",year,"_temp",sep=""))) { assign(paste("arunoff_",table_year, sep=""),get(paste("arunoff_",table_year,"_temp"))[c(10,7,9,5,4,12,1,3,2,8,11,6),]) table_year = table_year+1 } On Wed, Mar 25, 2009 at 1:58 PM, Steve Murray <smurray444 at hotmail.com> wrote:> > Dear all, > > I am trying to manually re-sort rows in a number of tables. The rows aren't sorted on any particular values but are simply ordered by user choice (as shown by the row numbers in the code). I have been able to carry out each re-arrangement without the use of the 'for' loop, but cannot seem to successfully execute the statements when incorporated into the loop. The code I have is as follows: > > table_year=1951 > for (i in (paste("arunoff_",year,"_temp",sep=""))) { > ? ? ? ? ?assign(paste("arunoff_",table_year, sep=""),paste("arunoff_",table_year,"_temp")[c(10,7,9,5,4,12,1,3,2,8,11,6),]) > ? ? ? ? ?table_year = table_year+1 > ? ? ? ? ?} > > > The error I get is: > > Error in paste("arunoff_", table_year, "_temp")[c(10, 7, 9, 5, 4, 12, ?: > ?incorrect number of dimensions > > ...depsite this not occurring when I do each table individually (so it can't be a case of there not being enough rows, as> dim(arunoff_1951_temp) gives [1] 12 11 > > I have a feeling that it may be a syntax error, possibly between 'temp' and the square bracket, but I can't be sure of this. > > > Any solutions or advice offered would be gratefully received. > > Many thanks, > > Steve > > _________________________________________________________________ > [[elided Hotmail spam]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
well, the literal answer is that paste("arunoff_",table_year,"_temp") is a character vector of length 1 so your indexing cannot work. What you want is to index the data that corresponds to this variable name, ?get But I should stress that this manipulation with assign and get seems completely unnecessary (not to mention that your for loop is quite redundant --- what's "i" for?). Did you try to use list() to collect your data instead of assign()? (as i suggested to you recently) Hope this helps, baptiste On 25 Mar 2009, at 17:58, Steve Murray wrote:> > Dear all, > > I am trying to manually re-sort rows in a number of tables. The rows > aren't sorted on any particular values but are simply ordered by > user choice (as shown by the row numbers in the code). I have been > able to carry out each re-arrangement without the use of the 'for' > loop, but cannot seem to successfully execute the statements when > incorporated into the loop. The code I have is as follows: > > table_year=1951 > for (i in (paste("arunoff_",year,"_temp",sep=""))) { > assign(paste("arunoff_",table_year, > sep=""),paste("arunoff_",table_year,"_temp") > [c(10,7,9,5,4,12,1,3,2,8,11,6),]) > table_year = table_year+1 > } > > > The error I get is: > > Error in paste("arunoff_", table_year, "_temp")[c(10, 7, 9, 5, 4, > 12, : > incorrect number of dimensions > > ...depsite this not occurring when I do each table individually (so > it can't be a case of there not being enough rows, as> > dim(arunoff_1951_temp) gives [1] 12 11 > > I have a feeling that it may be a syntax error, possibly between > 'temp' and the square bracket, but I can't be sure of this. > > > Any solutions or advice offered would be gratefully received. > > Many thanks, > > Steve > > _________________________________________________________________ > [[elided Hotmail spam]] > > ______________________________________________ > R-help@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._____________________________ Baptiste AuguiƩ School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag ______________________________ [[alternative HTML version deleted]]
I would suggest avoiding the function ?assign inside a loop. I used top use this until someone here kindly pointed out that it was much easier to catch the data of interest in a list... eg. df.list <- vector("list", length(10)) for (i in 1:10)} df.list[[i]]<-data.frame(arunoff_,table_year,_temp)[c(10,7,9,5,4,12,1,3,2,8,11,6),] } HTH Simon. ----- Original Message ----- From: "Steve Murray" <smurray444 at hotmail.com> To: <r-help at r-project.org> Sent: Wednesday, March 25, 2009 5:58 PM Subject: [R] Manual sort in a for loop> > Dear all, > > I am trying to manually re-sort rows in a number of tables. The rows > aren't sorted on any particular values but are simply ordered by user > choice (as shown by the row numbers in the code). I have been able to > carry out each re-arrangement without the use of the 'for' loop, but > cannot seem to successfully execute the statements when incorporated into > the loop. The code I have is as follows: > > table_year=1951 > for (i in (paste("arunoff_",year,"_temp",sep=""))) { > assign(paste("arunoff_",table_year, > sep=""),paste("arunoff_",table_year,"_temp")[c(10,7,9,5,4,12,1,3,2,8,11,6),]) > table_year = table_year+1 > } > > > The error I get is: > > Error in paste("arunoff_", table_year, "_temp")[c(10, 7, 9, 5, 4, 12, : > incorrect number of dimensions > > ...depsite this not occurring when I do each table individually (so it > can't be a case of there not being enough rows, as> dim(arunoff_1951_temp) > gives [1] 12 11 > > I have a feeling that it may be a syntax error, possibly between 'temp' > and the square bracket, but I can't be sure of this. > > > Any solutions or advice offered would be gratefully received. > > Many thanks, > > Steve > > _________________________________________________________________ > [[elided Hotmail spam]] > > ______________________________________________ > 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. >