Skip to content

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

5 messages · Tony Stocker, Sarah Goslee

#
Given this interactive session:
[1] "anova.ag.m2529.az"   "anova.ag.m2529.can"   "anova.ag.m2529.fl"
Analysis of Variance Table

Response: year
                 Df Sum Sq Mean Sq F value   Pr(>F)
time             1 14.823  14.8235    10.598 0.004392 **
Residuals   18  25.177   1.3987
---
Signif. codes:   0 '***'' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

How come the following just prints the names and not the contents of
the existing variable/data frame with the name assigned to x?  Does it
need to be dereferenced in some way to indicate that it's not a string
but an actual variable living inside the workspace?:
[1] "anova.ag.m2529.az"
[1] "anova.ag.m2529.can"
[1] "anova.ag.m2529.fl"

I'm trying to get this working interactively first, but ultimately I
need to put it into an Rscript so that I can use dynamic listings and
for loops to print everything.  I'd also be happy if I could pull just
the Pr out, as I could interactively like so:
[1] 0.004392059               NA

So what am I misunderstanding about how the language works?  I've been
all over the web and through the usingR.pdf file but can't find an
example that shows something like what I'm trying to do.  What exactly
(data frame, table, function, character string, etc.) is stored in
'anova.ag.m2529.az' if the commands that created it were:
Much thanks in advance.
#
Hi,

An anova object is stored in anova.ag.m2529.az.

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.

It's often neater and more efficient to store your anova objects in a
list, though.

Sarah
On Thu, Dec 15, 2011 at 9:30 AM, Tony Stocker <akostocker at gmail.com> wrote:

  
    
#
On Thu, Dec 15, 2011 at 09:51, Sarah Goslee <sarah.goslee at gmail.com> wrote:
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.
So if I were to do:
[1] FALSE
[1] TRUE
[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?

Again, thanks for the help!
#
Hi,
On Thu, Dec 15, 2011 at 10:43 AM, Tony Stocker <akostocker at gmail.com> wrote:
print(get(x)[["Pr"]]) maybe. Do the get(), then do the subsetting.
Yes: not the names, but the anova objects themselves. Rather than
creating a bunch of individual objects, store them in a list when
created:

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
#
On Thu, Dec 15, 2011 at 10:54, Sarah Goslee <sarah.goslee at gmail.com> wrote:
Okay I'll give that a try and see how it works for me.  Thanks for the
suggestion.