Last active
August 29, 2015 13:56
-
-
Save MonkmanMH/8798762 to your computer and use it in GitHub Desktop.
Percentile function in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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