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

Joshua Wiley jwiley.psych at gmail.com
Wed Oct 20 07:46:32 CEST 2010


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/



More information about the R-help mailing list