Re: [R] Loop with string variable AND customizable "summary" output

From: Vladimir Eremeev <>
Date: Mon 29 Jan 2007 - 15:50:12 GMT

C.Rosa wrote:
> Dear All,
> I am using R for my research and I have two questions about it:
> 1) is it possible to create a loop using a string, instead of a numeric
> vector? I have in mind a specific problem:
> for (i in c("UK","USA"))
> output{i}<-summary(lm(y{i} ~ x{i}))
> In other words, at the end I would like to have two objects as output:
> "outputUK" and "outputUSA", which contain respectively the results of the
> first and second regression (yUK on xUK and yUSA on xUSA).

Consider R functions bquote, substitute, eval and parse.

Several examples are given somewhere in RNews (
Unfortunately I don't remember exactly which issue, one of list members sent me a link to the article several years ago, when I was studying similar question.  

C.Rosa wrote:
> 2) I am thinking of something that is close in spirit to "summary" but it
> is also customizable. For example, suppose you want different Signif.
> codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 or a different
> format display (i.e. without "t value" column) implemented automatically
> (without manually editing it every time).
> In alternative, if I was able to see it, I could modify the source code of
> the function "summary", but I am not able to see its (line by line) code.
> Any idea?

Stars and significance codes are printed with the symnum function.

To customize the summary, explore the result returned by the lm. For example,

you will see, it is a list.
Then you will be able to reference its elements with $ (say, outputUK$coeff)

R is an object oriented language, and calls of the same function on different objects usually invoke different functions (if a class has a description of proper method).
The R manuals contain very good description of this mechanism.

Function methods gives you a list of all defined methods For example
> methods(summary)
> methods(print)

If you are working with the lm results, you need to explore the function print.summary.lm

> summary(outputUK)

invokes summary.lm function, as outputUK is the object of class "lm". This function produces the object of class "summary.lm" Then this object is printed with the method print.summary.lm

View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue Jan 30 02:57:11 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 29 Jan 2007 - 16:30:42 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.