[R] looping variable names

Greg Snow Greg.Snow at imail.org
Thu Feb 3 18:33:30 CET 2011


It is not clear what you are trying to do, but you can have lists (or vectors) of functions and that should simplify what you are trying to do:

> trigfuns <- list( s=sin, c=cos, t=tan )
> trigfuns
$s
function (x)  .Primitive("sin")

$c
function (x)  .Primitive("cos")

$t
function (x)  .Primitive("tan")

> sapply( trigfuns, function(f) f(pi/3) )
        s         c         t 
0.8660254 0.5000000 1.7320508 
>

And you can write a function that creates and returns a function:

> tmpfun <- function(a,b){
+ force(a); force(b)
+ function(x) {a + b * x}
+ }
> 
> myfuns <- vector('list',10*10)
> dim(myfuns) <- c(10,10)
> for(i in 1:10) {
+   for(j in 1:10) { 
+ myfuns[[i,j]] <- tmpfun(i,j)
+   }
+ }
> 
> myfuns[[2,3]](1:10)
 [1]  5  8 11 14 17 20 23 26 29 32
> myfuns[[3,2]](1:10)
 [1]  5  7  9 11 13 15 17 19 21 23
>

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Alaios
> Sent: Thursday, February 03, 2011 3:49 AM
> To: hypermonkey22; Eik Vettorazzi
> Cc: r-help at r-project.org
> Subject: Re: [R] looping variable names
> 
> Hello I would like to ask you if I can use the same method but for
> functions. That means that I want not to assign some value but a
> function.
> So is it possible to try something like that:
> 
> for (i in 1:10)  for (j in 1:10)
> assign(paste("var",i,j,sep=""),myfunction)
> 
> 
> 
> I would like to thank you in advance for your help
> 
> best Regards
> Alexandros
> --- On Thu, 2/3/11, Eik Vettorazzi <E.Vettorazzi at uke.uni-hamburg.de>
> wrote:
> 
> > From: Eik Vettorazzi <E.Vettorazzi at uke.uni-hamburg.de>
> > Subject: Re: [R] looping variable names
> > To: "hypermonkey22" <jonmosco at gmail.com>
> > Cc: r-help at r-project.org
> > Date: Thursday, February 3, 2011, 10:34 AM
> > As Greg wrote, a list is in most
> > circumstances a better way to store
> > many objects.
> > But you can use 'assign' and 'get' to create and access
> > (global) variables
> >
> > #creation
> > for (i in 1:100) assign(paste("var",i,sep=""),rnorm(5))
> >
> > #access i-th variable
> > i<-15
> > get(paste("var",i,sep=""))
> >
> > hth.
> >
> > Am 02.02.2011 21:36, schrieb hypermonkey22:
> > >
> > > Hi all,
> > >
> > > I've been looking for a simple answer to the following
> > problem.
> > >
> > > Let's say that I can loop through, say, 100 values
> > that need to be assigned
> > > to, say, the variables var1:var100.
> > >
> > > Is there an elegant way to do this?
> > >
> > > I have seen one or two similar questions...but they
> > tend to be in more
> > > complicated contexts.
> > > Simple question, hopefully with a simple answer.
> > >
> > > Thanks very much!
> >
> >
> > --
> > Eik Vettorazzi
> > Institut für Medizinische Biometrie und Epidemiologie
> > Universitätsklinikum Hamburg-Eppendorf
> >
> > Martinistr. 52
> > 20246 Hamburg
> >
> > T ++49/40/7410-58243
> > F ++49/40/7410-57790
> >
> > ______________________________________________
> > 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.
> >
> 
> 
> 
> 
> ______________________________________________
> 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.



More information about the R-help mailing list