Re: [R] another very simple loop question
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
Re: [R] another very simple loop question
Hi What about some example data? I presume you could use some apply, tapply, aggregate or similar function but without knowing what you **really** want to do and how your previous attempts failed it is hard to give any definite answer. e.g. mydf<-data.frame(year=sample(1:4,100,rep=T), student=sample(letters[1:4],100, rep=T), result=runif(100)) aggregate(mydf$result,list(year=mydf$year,student=mydf$student), mean) year student x 1 1 a 0.4026579 2 2 a 0.4690311 ... 153 d 0.5316301 164 d 0.4401949 > HTH Petr On 14 Apr 2006 at 0:09, Brian Quinif wrote: Date sent: Fri, 14 Apr 2006 00:09:32 -0400 From: "Brian Quinif" <[EMAIL PROTECTED]> 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 Petr Pikal [EMAIL PROTECTED] __ [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
