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

Vladimir Eremeev wl2776 at gmail.com
Mon Jan 29 16:50:12 CET 2007



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
(http://cran.r-project.org/doc/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, 
  str(outputUK)

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: http://www.nabble.com/-R--Loop-with-string-variable-AND-customizable-%22summary%22-output-tf3136358.html#a8691620
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list