Hi All I am pretty new to R but saw stata and sas's macro facilities and am looking for how such things work in R. I am trying to piece together a series of statements: n = 5 #want to have it dynamic with respect to n for (j in 1:n) { eval(paste("x", j, "=x[", j, "]", sep="")) } I want the created statements 'x1=x[1]' immediately executed and tried to do that with eval() but that did not work. Any hints greatly appreciates. Thanks Toby
Read the FAQ 7.21: http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f Note that it points out that you don't really want to do this anyways. On 3/9/07, toby909 at gmail.com <toby909 at gmail.com> wrote:> Hi All > > I am pretty new to R but saw stata and sas's macro facilities and am looking for > how such things work in R. > > I am trying to piece together a series of statements: > > > n = 5 #want to have it dynamic with respect to n > for (j in 1:n) { > eval(paste("x", j, "=x[", j, "]", sep="")) > } > > I want the created statements 'x1=x[1]' immediately executed and tried to do > that with eval() but that did not work. > > Any hints greatly appreciates. > > Thanks Toby > > ______________________________________________ > R-help at 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. >
toby909 at gmail.com wrote:> I am pretty new to R but saw stata and sas's macro facilities and am looking for > how such things work in R. > > I am trying to piece together a series of statements: > > n = 5 #want to have it dynamic with respect to n > for (j in 1:n) { > eval(paste("x", j, "=x[", j, "]", sep="")) > } > > I want the created statements 'x1=x[1]' immediately executed and tried to do > that with eval() but that did not work.IMO the macro-like features in R are less easy to understand than corresponding features e.g. in SAS. Perhaps it's their nature; perhaps it's that they are not documented in any single place, because they are integrated with the rest of the language. Others will disagree with my assessment. The very bright side is that because of the design of the S language, they are much less needed. As I read your example, you wish to assign an array element to a variable whose name is held in a character string. You can use the "assign()" function to do that. HTH. -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement.