lundi 10 septembre 2018

Multiple conditions with ifelse giving false answer in R

I'm looking at some EPL results and data on odds, taking some averages and looking at the results against the prices. The goal is that I have a column that satisfies two conditions on Home wins, two on Away.

The dataframe 'prem' is a cleaned up version of freely available data at footballdata.co.uk. I've taken out some irrelevant columns and turned the average odds columns (ave_a and ave_h) into numeric.

My code looks like:

prem$decision <- ifelse(prem$FTR == "H" & prem$ave_h < prem$ave_a * 2.5, prem$ave_h, ifelse(prem$FTR == "A" & prem$ave_a < prem$ave_h * 2.5, prem$ave_a, NA))

which gives me:

Date    HomeTeam        AwayTeam       FTR  ave_h   ave_a   decision
11/8/17 Arsenal         Leicester       H   1.5     6.51    1.5
12/8/17 Brighton        Man City        A   10.14   1.33    1.33
12/8/17 Chelsea         Burnley         A   1.24    14.18   NA
12/8/17 Crystal Palace  Huddersfield    A   1.82    4.82    NA
12/8/17 Everton         Stoke           H   1.7     5.51    1.7
12/8/17 Southampton     Swansea         D   1.62    5.95    NA
12/8/17 Watford         Liverpool       D   5.79    1.61    NA
12/8/17 West Brom       Bournemouth     H   2.42    3.13    2.42
13/8/17 Man United      West Ham        H   1.31    10.72   1.31
13/8/17 Newcastle       Tottenham       A   5.2     1.7     1.7
19/8/17 Bournemouth     Watford         A   1.96    4.01    4.01

but the decision doesn't return the NA when both conditions of the ifelse statement are met as can be seen in West Brom v Bournemouth or Bournemouth v Watford. This repeats through the whole season, negating the goal of the conditions.

Any ideas greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire