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

Chuck Cleland ccleland at optonline.net
Fri Feb 15 18:10:47 CET 2008


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



More information about the R-help mailing list