Hi, there I am a grad student struggling to get my syntax right in a model I am building in R. I am trying to specify a variable,W, in a loop, such that on the first iteration, it is equal to Wo, my starting value, which I have already specified with Wo<-9.2 On any other iteration other than the first, I want W to equal the previous value of W in the iteration. plus an increment from that previous iteration, divided by another value. This then becomes the value used in all calculations in the iteration at hand. Here is my code, that does not work: W<- function(w) { if (comp[i,1]=1) W[i]<-Wo else W[i]<-(work[i-1,5]work[i-1,13]*/Ef) } "work" is the dataframe that I am rbinding my interations into, and the W variable is the 5th variable in that frame, and the additive to W is the 13th variable in that frame. However, this is where the program stalls on me: { + + W<- function(w) + { + if (comp[i,1]=1) W[i]<-Wo else Error: syntax error Execution halted I've also attempted this variation; W<- function(w) if (comp[i,1]=1) {W[i]<-Wo} else W[i]<-(work[i-1,5]work[i-1,13]*/Ef) Which meets with an equal lack of success, and stalls in the same spot as the previous version. Does anyone have any hints on how to make this if else statement work to do what I need? This is my first time trying to use these statements, and the S-plus manual i have is proving to be terribly unhelpful. What am I doing wrong? Mike Michael Rennie M.Sc. Candidate University of Toronto at Mississauga 3359 Mississauga Rd. N. Mississauga, ON L5L 1C6 Ph: 905-828-5452 Fax: 905-828-3792 [[alternative HTML version deleted]]
Hi, there I am a grad student struggling to get my syntax right in a model I am building in R. I am trying to specify a variable,W, in a loop, such that on the first iteration, it is equal to Wo, my starting value, which I have already specified with Wo<-9.2 On any other iteration other than the first, I want W to equal the previous value of W in the iteration. plus an increment from that previous iteration, divided by another value. This then becomes the value used in all calculations in the iteration at hand. Here is my code, that does not work: W<- function(w) { if (comp[i,1]=1) W[i]<-Wo else W[i]<-(work[i-1,5]work[i-1,13]*/Ef) } "work" is the dataframe that I am rbinding my interations into, and the W variable is the 5th variable in that frame, and the additive to W is the 13th variable in that frame. However, this is where the program stalls on me: { + + W<- function(w) + { + if (comp[i,1]=1) W[i]<-Wo else Error: syntax error Execution halted I've also attempted this variation; W<- function(w) if (comp[i,1]=1) {W[i]<-Wo} else W[i]<-(work[i-1,5]work[i-1,13]*/Ef) Which meets with an equal lack of success, and stalls in the same spot as the previous version. Does anyone have any hints on how to make this if else statement work to do what I need? This is my first time trying to use these statements, and the S-plus manual i have is proving to be terribly unhelpful. What am I doing wrong? Mike Michael Rennie M.Sc. Candidate University of Toronto at Mississauga 3359 Mississauga Rd. N. Mississauga, ON L5L 1C6 Ph: 905-828-5452 Fax: 905-828-3792 [[alternative HTML version deleted]]
This is untested, so try it first, but my approach would be: W<- function(w) { W[i]<-ifelse(comp[i,1]==1, Wo, (work[i-1,5]work[i-1,13]*/Ef)) } Best, Andy Perrin ---------------------------------------------------------------------- Andrew J Perrin - http://www.unc.edu/~aperrin Assistant Professor of Sociology, U of North Carolina, Chapel Hill clists at perrin.socsci.unc.edu * andrew_perrin (at) unc.edu On Mon, 30 Jun 2003, Michael Rennie wrote:> > Hi, there > > I am a grad student struggling to get my syntax right in a model I am > building in R. > > I am trying to specify a variable,W, in a loop, such that on the first > iteration, it is equal to Wo, my starting value, which I have already > specified with > > Wo<-9.2 > > On any other iteration other than the first, I want W to equal the > previous value of W in the iteration. plus an increment from that previous > iteration, divided by another value. This then becomes the value used in > all calculations in the iteration at hand. > > Here is my code, that does not work: > > W<- function(w) > { > if (comp[i,1]=1) W[i]<-Wo else > W[i]<-(work[i-1,5]work[i-1,13]*/Ef) > } > > > "work" is the dataframe that I am rbinding my interations into, and the W > variable is the 5th variable in that frame, and the additive to W is the > 13th variable in that frame. > > However, this is where the program stalls on me: > > { > + > + W<- function(w) > + { > + if (comp[i,1]=1) W[i]<-Wo else > Error: syntax error > Execution halted > > I've also attempted this variation; > > W<- function(w) > > if (comp[i,1]=1) {W[i]<-Wo} else > W[i]<-(work[i-1,5]work[i-1,13]*/Ef) > > Which meets with an equal lack of success, and stalls in the same spot as > the previous version. > > Does anyone have any hints on how to make this if else statement work to do > what I need? This is my first time trying to use these statements, and the > S-plus manual i have is proving to be terribly unhelpful. > > What am I doing wrong? > > Mike > > > Michael Rennie > M.Sc. Candidate > University of Toronto at Mississauga > 3359 Mississauga Rd. N. > Mississauga, ON L5L 1C6 > Ph: 905-828-5452 Fax: 905-828-3792 > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
On Mon, Jun 30, 2003 at 11:21:52AM -0400, Michael Rennie wrote:> W<- function(w) > { > if (comp[i,1]=1) W[i]<-Wo else"==" is comparison operator, but "=" is assignment one. This code also doesn't work:> x <- 0 > if (x = 1)Error: syntax error But this works:> if (x == 1)+ Chack also help("=="), help("=") -- WBR, Timur.