[R] Am I misunderstanding loop variable assignment or how to use print()?

Sarah Goslee sarah.goslee at gmail.com
Thu Dec 15 16:54:28 CET 2011


On Thu, Dec 15, 2011 at 10:43 AM, Tony Stocker <akostocker at gmail.com> wrote:
> On Thu, Dec 15, 2011 at 09:51, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>> But "anova.ag.m2529.az"  is a character string that happens to be the
>> *name* of an anova object, but R has no way to know that unless you
>> specifically tell it that your character string is an object by using
>> get().
>> Something like print(get(x)) would work.
> Sarah - Thanks very much!  That did indeed work great at printing the
> entire contents out.  I couldn't do print(get(x$Pr)), but I can live
> with that for now.

print(get(x)[["Pr"]]) maybe. Do the get(), then do the subsetting.

>> It's often neater and more efficient to store your anova objects in a
>> list, though.
> So if I were to do:
>> is.list(an)
> [1] FALSE
>> alist<-list(an)
>> is.list(alist)
> [1] TRUE
>> alist
> [1] "anova.ag.m2529.az"   "anova.ag.m2529.can"   "anova.ag.m2529.fl"
> I would have created a list, but I'm assuming that you mean something
> different than that since I'm not sure how that functionally changed
> anything since it's still a set of character strings.  Could you
> elaborate a bit on what you mean by storing the anova objects as
> lists?

Yes: not the names, but the anova objects themselves. Rather than
creating a bunch of individual objects, store them in a list when

myanova <- list()
myanova[["ag.m2529.az"]] <- anova(whatever)
myanova[["ag.m2529.can"]] <- anova(whatever)

Then you can quite elegantly use lapply() across all of the anovas at once,
and don't have so many objects in your workspace.


Sarah Goslee

More information about the R-help mailing list