[R] another very simple loop question

Brian Koch bkoch at decisiondevelopment.com
Mon Apr 17 23:23:04 CEST 2006

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)):

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

for (u in c(as.list(x<-unique(data[,cvar])),list(x))) {
#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: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Brian Quinif
Sent: Thursday, April 13, 2006 11:10 PM
To: r-help at stat.math.ethz.ch
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

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?



R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list