4

I'm trying to loop through a list of numbers and concatenate them at the end of a variable.

for(i in c(3,6,9)) {
    x3=...
    x6=...
    x9=...  }

Is there a way to macro numbers using R such as something like x`i'?

Dennis
  • 14,264
  • 2
  • 48
  • 57
Matt
  • 119
  • 3
  • 6
  • 2
    It isn't clear to me what you're trying to do. Can you provide a little more detail. Maybe an example dataset with what your desired output looks like? – Dason Dec 13 '11 at 19:31
  • As Dason said, could you show an example of what you are trying to do? – Karel Bílek Dec 13 '11 at 19:33
  • 1
    I would encourage you to throw away any "programming" habits you've picked up from Stata and learn to use R in a more R-like way. – Joshua Ulrich Dec 13 '11 at 19:37
  • I basically want to run an equation on different lists of data in which the variable name only varies by the number at the end. Such as: x3=sum(x3) x6=sum(x6) x9=sum(x9) The function is more complex so it would be ideal to just loop through the trailing numbers and concatenate them to the end instead of rewriting the function every time. I have picked up that x`i' from Stata because it was a nice way to loop through numbers but am unsure of a similar method in R. – Matt Dec 13 '11 at 19:43
  • 2
    http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f – Ben Bolker Dec 13 '11 at 19:44
  • The explanation is helpful, but more context would definitely be more helpful in giving you an answer. The R idiom is generally to keep variables in a data structure (with names) such as a data frame that lets you work on the third, sixth, ninth ... element – Ben Bolker Dec 13 '11 at 19:51
  • 3
    @Matt: Please edit a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) into your question. You're clearly new to R and are not using terminology R users will understand (e.g. you want to "apply a function" not "run an equation"; "lists of data" could be a data.frame, a list of vectors, a list of data.frames, etc.). A reproducible example will clarify your intent. – Joshua Ulrich Dec 13 '11 at 19:52
  • I recommend Ben B's first response become the answer. All the same, Joshua is correct: if you want to do this sort of list-wise function application, you probably have no need to create that set of names in the first place. – Carl Witthoft Dec 13 '11 at 19:55
  • I recommend article: Programmer’s Niche: Macros in R. The Newsletter of the R Project Volume 1/3, September 2001 (www.r-project.org). – Wojciech Sobala Dec 13 '11 at 20:21
  • @Matt I agree with the above comments. I know Stata and when I finally moved to R, it took me a while to figure out why there weren't macros (in the Stata sense). But once you learn the R way, you start to see how silly Stata is. You need to do a lot of unintuitive tricks in Stata. I find R much more intuitive. But to get to that point, I had to learn a lot about R first. – Xu Wang Dec 13 '11 at 20:22
  • @XuWang, @CarlWitthoft, @WojciechSobala if you think using `assign` in a loop is the answer to this question, please vote to close it as a duplicate of [one of these questions](http://stackoverflow.com/search?q=[r]+loop+assign) (especially given the answer is also in the R-FAQ @BenBolker linked to). – Joshua Ulrich Dec 13 '11 at 20:38
  • @JoshuaUlrich I do think that assign is the answer. I don't see how to vote to close. Do I have that privilege? And I'm not sure this is a duplicate question. I agree the answers will be duplicates of the answers in the questions you linked to but to me the real question here is "How can I write a Stata macro in R?" I think the question should be edited to include the word Stata and then I think it is an original question. Or is there already a Stata macro question? Am I wrong? I trust your judgment so if you still recommend a close and you can tell me how to vote for that, I'll gladly do it. – Xu Wang Dec 13 '11 at 20:47
  • @XuWang: IMO it would be up to the original poster to decide if the question should be changed as you suggest. Casting close & open votes takes 3000 reputation: http://stackoverflow.com/privileges/close-questions – Ben Bolker Dec 13 '11 at 20:57
  • @BenBolker I agree, but often times more experienced users should suggest edits to him/her because as a poster sometimes it's difficult to ask what you really mean. So to me the question is should we encourage him to edit the question or should we vote to close? I usually prefer the first one because someone could search for Stata and macro and not find anything. – Xu Wang Dec 13 '11 at 21:03
  • 1
    In any case, I consider this question to be a duplicate of this question (which does mention Stata. The question does not mention "macro" but an answer does): http://stackoverflow.com/questions/5024437/how-to-rewrite-this-stata-code-in-r – Xu Wang Dec 13 '11 at 21:04
  • I can't vote to close, but I can delete my answer :) – Xu Wang Dec 13 '11 at 21:04

1 Answers1

3

It is possible but not recommended:

for(i in c(3,6,9))
 assign(paste("a",i,sep=""),value=...)
Wojciech Sobala
  • 7,431
  • 2
  • 21
  • 27