[R] piecing together statements (macro?)

Mike Prager mike.prager at noaa.gov
Mon Mar 12 16:21:01 CET 2007


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.



More information about the R-help mailing list