vendredi 27 novembre 2015

Missing value error in if statement

I have a data.frame like this

home <- c("MANU","CHELSEA")
away <- c("SWANSEA", "LIVERPOO")
GH <- c(3,4)
GA <- c(2,1)

df <- data.frame(home, away, GH, GA)

I would like add a column in the df which fills a point column based on the result:

calc <- function(df) {

 df$POINTS <- 0

 for(i in 1:length(df))

  if(df$GA[i] > df$GH[i]) {
    df$POINTS[i] <- 0.11
  }
  else {
    df$POINTS[i] <- 0.22
    print("a")
  }

}

Running the formule gives this error however:

> calc(df)
Error in if (df$GA[i] > df$GH[i]) { : 
missing value where TRUE/FALSE needed

Could anybody explain me what is going wrong here? The if statement is evaluating to T/F right?

Aucun commentaire:

Enregistrer un commentaire