[R] is get() really what I want here?

Petr PIKAL petr.pikal at precheza.cz
Wed Oct 20 08:36:01 CEST 2010


Hi

r-help-bounces at r-project.org napsal dne 20.10.2010 07:58:29:

> Hi Josh,
> What I'm really trying to do is to refer to objects whose names I have
> stored in a vector.  This example was arbitrary.
> I do a lot of looping through files in the working directory, or through
> objects in the namespace, and I'm confused about how best to call upon 
them
> from within a loop.

Why not create those objects as part of list and loop though list.

regards
Petr


> Thanks,
> Dan
> 
> On Wed, Oct 20, 2010 at 4:46 PM, Joshua Wiley 
<jwiley.psych at gmail.com>wrote:
> 
> > Hi Daniel,
> >
> > get() will work for any object, but cat() may not.  cat() should work
> > for arrays, but it will be messy even for relatively small ones.  For
> > example, run:
> > cat("Hello", array(1:100, dim = c(10, 10)), sep = " ")
> >
> > What are you really trying to do?  If you are just trying to figure
> > out what random variables in your workspace you've assigned but do not
> > know/forgot what they are, consider:
> >
> > ls.str(pattern="^obj")
> >
> > as a better way to get their names and some useful summaries
> > (including class and number of observations).
> >
> > HTH,
> >
> > Josh
> >
> > On Tue, Oct 19, 2010 at 10:29 PM, Daniel Weitzenfeld
> > <dweitzenfeld at gmail.com> wrote:
> > > # Let's say I have 5 objects, object_1, object_2, etc.
> > > for (i in 1:5) {
> > >    assign(paste("object_",i, sep=""), i+500)
> > > }
> > >
> > > # Now, for whatever reason, I don't know the names of the objects 
I've
> > > created, but I want to operate on them.
> > > list<-ls(pattern="^obj")
> > >
> > > #Is get best?
> > > for (l in list) {
> > >    cat("\n", l, "is", get(l), sep=" ")
> > > }
> > >
> > > Is get() the correct command to use in this situation?  What if 
rather
> > than
> > > just an integer, object_1 etc are large arrays - does that change 
the
> > > answer, for speed reasons?
> > >
> > > Thanks in advance,
> > > Dan
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > 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.
> > >
> >
> >
> >
> > --
> > Joshua Wiley
> > Ph.D. Student, Health Psychology
> > University of California, Los Angeles
> > http://www.joshuawiley.com/
> >
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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