mardi 25 août 2020

Alternative to ifelse for three options

So I have a database with football(soccer) results. I have a column with difference between home and away team and want to create a column which tells whether the home team won, lost or drew.

So I wrote an if-else statement

if (difference>0) {
  result2 = 'win'
} else if (difference<0) {
  result2 = 'loss'
} else if (difference==0) {
  result2 = 'draw'
} else {
  result2 = 'error'
}

Howerver, this if-else statement is not possible since if is not vectorized in R. And when I use ifelse() I can only have two outcomes, so only win or loss for example. Is there a simple way to overcome this problem?

Aucun commentaire:

Enregistrer un commentaire