Skip to content

Instantly share code, notes, and snippets.

@trinker
Created June 22, 2018 12:36
Show Gist options
  • Save trinker/c9d3dc74fa9595f4562f4a3eaa2a4a91 to your computer and use it in GitHub Desktop.
Save trinker/c9d3dc74fa9595f4562f4a3eaa2a4a91 to your computer and use it in GitHub Desktop.
Computing percentiles
percentile1 <- function(x) ecdf(x)(x)
percentile2 <- function(x) rank(x)/length(x)
library(tidyverse)
out <- data_frame(
x = rnorm(100000),
ecdf = ecdf(x)(x),
rank_len = rank(x)/length(x),
same = near(ecdf, rank_len)
)
x1 <- sort(rnorm(50, mean = .07, sd = .06)); x1 <- ifelse(x1 < 0, 0, x1)
percentile1(x1)
percentile2(x1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment