Re: [R] Macros in R
Others have pointed you to the answer to your question, but both FAQ 7.21 and the assign help page should really have a big banner at the top saying Here Be Dragons. Using a loop or other automated procedure to create variables in the main namespace can cause hard to find bugs, accidentally clobber existing variables, and other non-fun things. For this type of thing it is usually best to use a list (or an environment, but I am more comforatable with lists). For your example you could do something like: mymats - list() for (i in 1:54){ + myname - paste('mymatrix',i,sep='') + mymats[[myname]] - matrix( # insert whatever code you want here + } A big advantage of this approach is that you can then deal with your list of matricies as a single unit. If you want to delete them, you just delete the list rather than having to delete 54 individual matricies. The list can also be saved as a single unit to a file, passed to another function, etc. To access a single matrix (for example 'mymatrix5' which is in position 5) you have several options: mean( mymats[[5]] ) mean( mymats[['mymatrix5']] ) with( mymats, mean(mymatrix5) ) attach(mymats) mean(mymatrix5) # as long as there is not a mymatrix 5 in the global environment detach() And probably others. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Monika Kerekes Sent: Sunday, February 25, 2007 9:03 AM To: r-help@stat.math.ethz.ch Subject: [R] Macros in R Dear members, I have started to work with R recently and there is one thing which I could not solve so far. I don't know how to define macros in R. The problem at hand is the following: I want R to go through a list of 1:54 and create the matrices input1, input2, input3 up to input54. I have tried the following: for ( i in 1:54) { input[i] = matrix(nrow = 1, ncol = 107) input[i][1,]=datset$variable } However, R never creates the required matrices. I have also tried to type input'i' and input$i, none of which worked. I would be very grateful for help as this is a basic question the answer of which is paramount to any further usage of the software. Thank you very much Monika [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Macros in R
The FAQ does mention your point already. On 2/27/07, Greg Snow [EMAIL PROTECTED] wrote: Others have pointed you to the answer to your question, but both FAQ 7.21 and the assign help page should really have a big banner at the top saying Here Be Dragons. Using a loop or other automated procedure to create variables in the main namespace can cause hard to find bugs, accidentally clobber existing variables, and other non-fun things. For this type of thing it is usually best to use a list (or an environment, but I am more comforatable with lists). For your example you could do something like: mymats - list() for (i in 1:54){ + myname - paste('mymatrix',i,sep='') + mymats[[myname]] - matrix( # insert whatever code you want here + } A big advantage of this approach is that you can then deal with your list of matricies as a single unit. If you want to delete them, you just delete the list rather than having to delete 54 individual matricies. The list can also be saved as a single unit to a file, passed to another function, etc. To access a single matrix (for example 'mymatrix5' which is in position 5) you have several options: mean( mymats[[5]] ) mean( mymats[['mymatrix5']] ) with( mymats, mean(mymatrix5) ) attach(mymats) mean(mymatrix5) # as long as there is not a mymatrix 5 in the global environment detach() And probably others. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Monika Kerekes Sent: Sunday, February 25, 2007 9:03 AM To: r-help@stat.math.ethz.ch Subject: [R] Macros in R Dear members, I have started to work with R recently and there is one thing which I could not solve so far. I don't know how to define macros in R. The problem at hand is the following: I want R to go through a list of 1:54 and create the matrices input1, input2, input3 up to input54. I have tried the following: for ( i in 1:54) { input[i] = matrix(nrow = 1, ncol = 107) input[i][1,]=datset$variable } However, R never creates the required matrices. I have also tried to type input'i' and input$i, none of which worked. I would be very grateful for help as this is a basic question the answer of which is paramount to any further usage of the software. Thank you very much Monika [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Macros in R
The FAQ does mention using a list (and I did not mean to imply that it did not). Personally I think it is a little soft on this point for the following reasons: 1. It mentions lists at the very end, some users may read about the assign function, think that that answers the question that they think they have, and never read on to the end. 2. The phrase often easier seems a soft sell to me, like consider this, not DO IT THIS WAY. 3. It does not point out any of the dangers of using assign and the additional benefits of using lists. 4. The phrase Here Be Dragons is fun to say, and seeing it grabs attention and gets people thinking (except maybe on September 19th, International Talk Like a Pirate Day). And the parenthetical remark on number 4 brings up the obvious addition to the R Infrequently Asked Questions list: Q: Aye, mateys, what be a pirate's favorite statistical package? A: R, of course (but you need to pronounce it R :-). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 27, 2007 12:11 PM To: Greg Snow Cc: Monika Kerekes; r-help@stat.math.ethz.ch Subject: Re: [R] Macros in R The FAQ does mention your point already. On 2/27/07, Greg Snow [EMAIL PROTECTED] wrote: Others have pointed you to the answer to your question, but both FAQ 7.21 and the assign help page should really have a big banner at the top saying Here Be Dragons. [snip] __ R-help@stat.math.ethz.ch 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.
Re: [R] Macros in R
If I understand the question correctly, I would do this: for (i in 1:54) assign( paste('input',i,sep='') , matrix( dataset$variable, nrow=1) ) You now have 54 matrices, named input1, input2, ... input54, each having 1 row and as many columns as dataset$variable is long. (also, they're identical, since all are created from the same object, dataset$variable) See, of course, the help page for assign() to see why this works. However, I do wonder, in the bigger picture of what you're trying to do, whether there isn't a better way. For example, why matrices, since they all have only one row? -Don At 5:02 PM +0100 2/25/07, Monika Kerekes wrote: Dear members, I have started to work with R recently and there is one thing which I could not solve so far. I don't know how to define macros in R. The problem at hand is the following: I want R to go through a list of 1:54 and create the matrices input1, input2, input3 up to input54. I have tried the following: for ( i in 1:54) { input[i] = matrix(nrow = 1, ncol = 107) input[i][1,]=datset$variable } However, R never creates the required matrices. I have also tried to type input'i' and input$i, none of which worked. I would be very grateful for help as this is a basic question the answer of which is paramount to any further usage of the software. Thank you very much Monika [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. -- -- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA __ R-help@stat.math.ethz.ch 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.
Re: [R] Macros in R
Its a FAQ. http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f On 2/25/07, Monika Kerekes [EMAIL PROTECTED] wrote: Dear members, I have started to work with R recently and there is one thing which I could not solve so far. I don't know how to define macros in R. The problem at hand is the following: I want R to go through a list of 1:54 and create the matrices input1, input2, input3 up to input54. I have tried the following: for ( i in 1:54) { input[i] = matrix(nrow = 1, ncol = 107) input[i][1,]=datset$variable } However, R never creates the required matrices. I have also tried to type input'i' and input$i, none of which worked. I would be very grateful for help as this is a basic question the answer of which is paramount to any further usage of the software. Thank you very much Monika [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.