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

Joshua Wiley jwiley.psych at gmail.com
Wed Oct 20 08:37:37 CEST 2010


On Tue, Oct 19, 2010 at 10:58 PM, Daniel Weitzenfeld
<dweitzenfeld at gmail.com> wrote:
> 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.

get() should be fine for this.  If the files in the working directory
are related enough that you can loop through them, you might also
consider putting them in a (named) list.  Here are some examples how
that might work:

object <- vector(mode = "list", length = 5)
for (i in 1:5) {
    object[[i]] <- i + 500
}

for(i in 1:5) {
  print(object[[i]])
}

# More complex
object2 <- list(A = data.frame(names = c("Mary", "John"), scores = c(5, 4)),
                B = matrix(1:100, ncol = 10),
                Times = Sys.time() + 0:1)

for (i in 1:3) {
    print(summary(object2[[i]]))
}

# But also
object2[[2]]
object2[["Times"]]

> 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/
>
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list