Re: [R] problems with for loop

From: Chuck Cleland <ccleland_at_optonline.net>
Date: Tue 16 Jan 2007 - 15:25:44 GMT

Jorge Lampurlanes Castel wrote:
> Hello,
>
> With this program I try to repeat analysis for different years. The
> results of the analysis are not printed when in the loop, except for the
> year sequence. What is wrong?
>
> Thanks a lot.
>
> for (i in 92:99){
> cat("\n",
> "=============================================================\n",
> "YEAR =",i,"\n",
> "=============================================================\n"
> )
> canos1 <- subset (canos, canos$YEAR==i)
> LinearModel.1 <- lm(MM_P ~ BLOC + TIL, data=canos1)
> summary(LinearModel.1)
> Anova(LinearModel.1, type="II")
> Anova(LinearModel.1, type="III")
>
> }

  You might consider using by() rather than a loop as follows:

library(car)

by(canos, canos$YEAR,

   function(x){fm <- lm(MM_P ~ BLOC + TIL, data=x)    ans <- list(summary(fm), Anova(fm, type="II"), Anova(fm, type="III"))    }
)

  The result can be conveniently stored as a list of lists.

-- 
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@stat.math.ethz.ch 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 Wed Jan 17 02:32:43 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 16 Jan 2007 - 16:30:27 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.