Skip to content

if else statements in R

2 messages · Michael Rennie, Kjetil Halvorsen

#
On 30 Jun 2003 at 11:24, Michael Rennie wrote:
Hola!
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!
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