Where is the percentile function in CRAN -R
I do not understand all t开发者_运维知识库he terminology inside R. I have only 100 level statistics, trying to learn more.
I am guessing R has a built-in percentile function named something I don't recognize or know how to search for.
I can write my own, but rather use the built in one for obvious reasons.
Here's the one I wrote:
percentile <- function(x) return((x - min(x)) / (max(x) - min(x))
You can do this via
scale(x,center=min(x,na.rm=TRUE),scale=diff(range(x,na.rm=TRUE)))
but I'm not sure there is actually a built-in function that does the scaling you're asking for.
If you are looking to find out specific percentiles from a data set, take a look at the quantile
function: ?quantile
. By multiplying by 100, you get percentiles.
If you are looking into converting numbers to their percentiles, take a look at rank
, though you will need to determine how to address ties. You can simply rescale from rank to quantile by dividing by the length of the vector.
The quantile
function might be what you are looking for. If you have vector x
and you want to know the 25th, 43rd, and 72nd percentiles you would execute this:
quantile(x, c(.25, .43, .72));
The semicolon is, of course, optional.
See http://www.r-tutor.com/elementary-statistics/numerical-measures/percentile
You can search for functions (or for just about anything else) via RSiteSearch
e.g.,
RSiteSearch("percentile")
On the off chance you are thinking about a percentile based on a distribution, here is a different answer. Each probability distribution has a set of 4 functions associated with it: a density, distribution, quantile, and generating function. These are prefixes of d-, p-, q-, and r-, respectively (with the same suffix based on the distribution). You have a uniform distribution, and are asking about percentiles (distribution) so you want punif
. It takes min
and max
as two of its arguments.
I made this function function, check it. Data is any vector, row of any matrix o data frame.
percentiles<-function(Data) return(quantile(Data, seq(0,1, by=.01)))
精彩评论