Skip to content

Instantly share code, notes, and snippets.

@selftext
Last active August 29, 2015 14:23
Show Gist options
  • Save selftext/560662fb7827178fffcf to your computer and use it in GitHub Desktop.
Save selftext/560662fb7827178fffcf to your computer and use it in GitHub Desktop.
Generate a standard normal distribution to import into a database as a lookup table
library(dplyr)
# create data frame of z-scores and probabilities
standard_normal_distribution <- data.frame(z_score = seq(-7, 7, 0.000001),
probability = pnorm(seq(-7, 7, 0.000001)))
# find max number of characters (redshift has a limit of DECIMAL(38, 37))
max(nchar(standard_normal_distribution$probability))
# if necessary, round big decimals
standard_normal_distribution <- standard_normal_distribution %>%
mutate(rounded = round(probability, 20)) %>%
select(z_score, rounded)
names(standard_normal_distribution) <- c("z_score", "probability")
write.csv(standard_normal_distribution,
file = "standard_normal_distribution.csv",
row.names = FALSE)
# sample of output
head(standard_normal_distribution, 10)
# z_score probability
# 1 -7.000000 1.279813e-12
# 2 -6.999999 1.279822e-12
# 3 -6.999998 1.279831e-12
# 4 -6.999997 1.279840e-12
# 5 -6.999996 1.279849e-12
# 6 -6.999995 1.279858e-12
# 7 -6.999994 1.279867e-12
# 8 -6.999993 1.279876e-12
# 9 -6.999992 1.279886e-12
# 10 -6.999991 1.279895e-12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment