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

Tony Stocker akostocker at gmail.com
Thu Dec 15 15:30:00 CET 2011

Given this interactive session:

> an<-ls(pat="anova.ag.m2529")
> an
 [1] "anova.ag.m2529.az"   "anova.ag.m2529.can"   "anova.ag.m2529.fl"
> print(anova.ag.m2529.az)
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?:

> for(x in an) {print(x)}
[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:

> print(anova.ag.m2529.az$Pr)
[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:

> aov.m2529.az=aov(year~time,data=ag.m2529.az)
> anova.ag.m2529.az=anova(aov.m2529.az)

Much thanks in advance.

More information about the R-help mailing list