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

From: Roger Bivand <>
Date: Mon 29 Jan 2007 - 15:31:01 GMT

On Mon, 29 Jan 2007 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:
> Suppose you have 2 countries: UK, and USA, one dependent (y) and one
> independent variable (y) for each country (vale a dire: yUK, xUK, yUSA,
> xUSA) and you want to run automatically the following regressions:
> 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).

The input data could be reshaped as y, x, country, and subset= used in the lm() call. To assign to named objects see assign(), but consider using a named list instead, assigning to a list of the required length in turn, and giving the names from the defining vector. Then you'd get output$UK, etc.

> 2) in STATA there is a very nice code ("outreg") to display nicely (and
> as the user wants to) your regression results.
> Is there anything similar in R / R contributed packages? More precisely,
> 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?

Use a custom function on the output object from using the summary() method on the lm object (that is on the summary.lm object). Use str() to look at the summary.lm object to see what you want.

> Or may be a customizable regression output already exists?
> Thanks really a lot!
> Carlo
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.

Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Tue Jan 30 02:41:30 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.