In marketing research, I often need to loop through highly customized
routines similar to what you described ("all" then "each," or
vice-versa).  My "trick" is to loop through c(as.list(x),list(x)):

data<-your_data_frame
cvar<-your_classification_varname #where cvar %in% colnames(data)

for (u in c(as.list(x<-unique(data[,cvar])),list(x))) {
local({
data<-data[is.element(data[,cvar],u),]
#all your statements here inside the local() block
})}

This runs through all my expressions once per level of the classvar, and
then once without classification.  Add statements in here to manage NA's
(if applicable) during the extraction, and you're good to go.

-Brian J. Koch
Data Manager
Decision Development Inc





-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Brian Quinif
Sent: Thursday, April 13, 2006 11:10 PM
To: [email protected]
Subject: [R] another very simple loop question

I have a dataset with 4 years of students, and normally I want to
estimate things using each individual year, so I have a for loop as
follows

for (i in 1:4){}

However, the only way I know how to calculate estimates using all four
years of data is to put the estimations outside of the loop.  Is there
anyway to make a for loop that uses all four years at once, then uses
each individual year?

Thanks,

BQ

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to