Skip to content

Instantly share code, notes, and snippets.

@MonkmanMH
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MonkmanMH/8798762 to your computer and use it in GitHub Desktop.
Save MonkmanMH/8798762 to your computer and use it in GitHub Desktop.
Percentile function in R
# CALCULATING PERCENTILES IN R
#
# a basic percentile function using "ecdf" [Empirical Cumulative Distribution Function]
# using a data file "percentiledata" with variable VALUE
percentileFUN <- ecdf(percentiledata$VALUE)
percentileFUN
percentileFUN(percentiledata$VALUE)
# write the percentile values to the source file
percentiledata$pctl <- percentilefunction(percentiledata$VALUE)
#
#
#
# to run separate percentiles on the VALUE by the groups in the data (variable GROUP)
# uses the "mutate" function in plyr
# (see http://www.seananderson.ca/2013/12/01/plyr.html)
require(plyr)
newpercentiledata <- ddply(percentiledata, "GROUP", mutate, pctl ={
percentileFUN <- ecdf(VALUE)
percentileFUN
percentileFUN(VALUE)
})
#
#
# version for WES2013
#
newWU_driver_melt <- ddply(WU_driver_melt, "variable", mutate, pctl ={
percentileFUN <- ecdf(value)
percentileFUN
percentileFUN(value)
})
#
# REFERENCES
# http://www.jstatsoft.org/v40/i01
# http://www.seananderson.ca/2013/12/01/plyr.html
#
})
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment