Re: [R] lmList, tapply() and lm()

From: Chuck Cleland <ccleland_at_optonline.net>
Date: Fri, 15 Feb 2008 12:10:47 -0500

On 2/15/2008 11:00 AM, Marc Belisle wrote:
> Howdee,
>
> *** I know that the lmList() function exists, yet I don't want to use it.
> ***
>
> Would anyone be kind enough to tell how I can apply the function lm() to
> each level of a given factor so to obtain the intercept and slope for each
> factor level within a matrix?
>
> For instance, suppose a dataframe containing 3 variables: id, x and y.
>
> I want to compute the function lm() for each level contained in id, as
> lmList would do...

   Something like this?

t(sapply(split(df, list(df$id)),

    function(subd){coef(lm(y ~ x, data = subd))}))

> Thanks for your time,
>
> Marc
>
> ===================
> Marc Bélisle
> Professeur adjoint
> Chaire de recherche du Canada en écologie spatiale et en écologie du paysage
> Département de biologie
> Université de Sherbrooke
> 2500 Boul. de l'Université
> Sherbrooke, Québec
> J1K 2R1 Canada
>
> Tél: +1-819-821-8000 poste 61313
> Fax: +1-819-821-8049
> Courriél: Marc.M.Belisle_at_USherbrooke.ca
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 15 Feb 2008 - 17:24:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 15 Feb 2008 - 17:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive