[R] Help with accessing elements of a list

Prof Brian D Ripley ripley at stats.ox.ac.uk
Mon Feb 4 15:49:13 CET 2002


sapply(wg, coef) will give you a matrix of coefficients for all 293 fits,
I believe.

Do use the inbuilt extractors like coef to get coefficients: direct access
can give the wrong results.

On Mon, 4 Feb 2002, anthony staines wrote:

> Hi,
>
> As part of the analysis of a longitudinal study, I have a list, called wg,
> on R 1.3.1 for windows created as follows -
>
> wg <- by(xx, xx$mrn, function(xx) lm(xx$maternal.wt ~ xx$gestation,
> data=xx))
>
> xx is a data.frame, and so wg contains the results of 293 linear
> regressions, as lm objects.
>
> Now what I want is to extract the 293 intercepts and regression coefficients
> as a vector, and do things with them - like examine their distributions.
>
> >wg[[1]] gives
>
> Call:
> lm(formula = xx$maternal.wt ~ xx$gestation, data = xx)
>
> Coefficients:
> (Intercept)  xx$gestation
>      61.3014        0.2886
>
> >wg[[1]][[1]] gives
>
> (Intercept) xx$gestation
>    61.301422     0.288568
>
> >wg[[1]][[1]][[2]] gives
>
> [1] 0.288568
>
> What I need to get is 293 of these in a vector. None of the obvious
> variations on
>
> >wg[[1:293]][[1]][[2]] gives anything except -
>
> Error: attempt to select more than one element
>
> I've read the manuals, and selections of the reference manual without
> achieving wisdom. I'm fairly sure that this is a very easy question, but I
> can't work it out by myself - and my head now hurts. Any suggestions?
>
> --
> Anthony Staines
> Please reply to my work e-mail address anthony.staines at ucd.ie
>
> _________________________________________________________________
> Join the world’s largest e-mail service with MSN Hotmail.
> http://www.hotmail.com
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list