Skip to content

Instantly share code, notes, and snippets.

@JakeRuss
Created October 26, 2014 23:17
Show Gist options
  • Save JakeRuss/59bbe9fd252cfc72ac99 to your computer and use it in GitHub Desktop.
Save JakeRuss/59bbe9fd252cfc72ac99 to your computer and use it in GitHub Desktop.
Convert decimal betting odds to American format
# Convert decimal betting odds to American format
#
toAmericanOdds <- function(x) {
if (!is.numeric(x))
x <- as.numeric(x)
# For decimal odds greater than or equal to 2,
# American odds = (decimal odds – 1) * 100
x[x >= 2.0 & !is.na(x)] <- (x[x >= 2.0 & !is.na(x)] - 1) * 100
# For decimal odds less than 2,
# American odds = (-100) / (decimal odds - 1)
x[x < 2.0 & !is.na(x)] <- (-100) / (x[x < 2.0 & !is.na(x)] - 1)
# Remove decimal places
x <- round(x)
return(x)
} # end function
# Sample odds
odds <- c("1.892", "2.000", "1.925", "1.961",
"1.934", "1.952", NA, "2.020")
# Test function
toAmericanOdds(odds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment