lundi 26 février 2018

multiple statement in function

I would like to create a function with 2 statements (if and else if).I have 26 Swiss cantons and the 27th is the Swiss government (it should be the sum of all other cantons). By running my code, I obtain exactly what I want (a vector with 27 values), except that the second statement (else if), doesn't work. I have 0 (because I set at the begining sep = 0) instead of the sum of all the previous 26 cantons.

So I should do something wrong in the second statement. Here is my code

A.function = function(p){
  sep <- 0
  for(i in 1:length(Canton))
    if(IR[i] < 100) {
    sep[i] <- (100-IR[i])^(1+p)*Pop[i]
    }
    else if(IR[i] = 100) {
    sep[i] <- sum(sep)
    }

  return(sep)
}

Anyone could help me?

Aucun commentaire:

Enregistrer un commentaire