[R] Newbie programming help

Chuck Cleland ccleland at optonline.net
Fri Aug 22 23:43:53 CEST 2008


On 8/22/2008 5:34 PM, Ranney, Steven wrote:
> All - 
> 
> Not sure if this is a real programming question, but here goes:
> 
> I have data that looks like
> 
> Lake	Length	Weight
> 1	158	45
> 1	179	70
> 1	200	125
> 1	202	150
> 1	206	145
> 1	209	165
> 1	210	140
> 1	215	175
> 1	216	152
> 1	220	150
> 1	221	165
> ...
> 
> where lake goes from 1 - 84 and the number of rows for each lake is variable (but > ~20).  
> I'm trying to do two things: 1) build a simple linear model of the form 
> 
> {lm(log10(Weight)~log10(Length)}
> 
> for every separate lake in the data set; 2) I'd like to save the intercepts and slopes 
> from each of these linear regressions into a seperate data frame.  Any ideas?  I think it would 
> probably require some kind of 'for' statement, but I'm just not that smart.

  Assuming the data are in a data frame called "mydf":

library(nlme)

fm1 <- lmList(log10(Weight)~log10(Length) | Lake, mydf)

coef(fm1)

?lmList

or

t(sapply(split(mydf, mydf$Lake),
function(x){coef(lm(log10(Weight)~log10(Length), data=x))}))

> Thanks for your help, 
> 
> SR  
> 
> Steven H. Ranney
> Graduate Research Assistant (Ph.D)
> USGS Montana Cooperative Fishery Research Unit
> Montana State University
> PO Box 173460
> Bozeman, MT 59717-3460
> 
> phone: (406) 994-6643
> fax:   (406) 994-7479
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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. (www.ndri.org)
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