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

Sarah Goslee sarah.goslee at gmail.com
Thu Dec 15 15:51:14 CET 2011


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:
> 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.
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list