[R] if else statements in R

kjetil brinchmann halvorsen kjetil at entelnet.bo
Mon Jun 30 18:14:35 CEST 2003


On 30 Jun 2003 at 11:24, Michael Rennie wrote:

Hola!

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

There are multiple problems with this code
1)  You use W as a name of a function AND as a name of an array 
inside that function. Might not be a syntax error (?), but is asking 
for problems. Use an other name for your array, and assign it first.
2) The variable i is not given a value anywhere in your code.
3)The condition in the if statement uses = and not ==, = is 
assignment, == is test lfor equality. Actuallt, your comp[i,1] gets 
assigned 1, that is also the value of the assignment, and in the 
logical test the 1 will be interpreted as TRUE, so you will never get 
the else branch!

> 
> W<- function(w)
> {
> if (comp[i,1]=1) W[i]<-Wo else
> W[i]<-(work[i-1,5]work[i-1,13]*/Ef)
> }

Maybe youwant something like:

W <- function(w) {
    result <- numeric(length=?)
    if (comp[i,1]==1) result[i] <- W0 else
          result[i] <- work[i-1,5] * work[i-1,13]/Ef
}

but it is really impossible to tell, as your function are using 
global variables. Another good idea is to avoid global variables.

Kjetil Halvorsen


> 
> 
> "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




More information about the R-help mailing list