[R] Getting empirical percentiles for data

Christoph Buser buser at stat.math.ethz.ch
Fri Jan 7 09:08:59 CET 2005


Dear Mike

Mike Cheung writes:
 > Dear List,
 > 
 > I have some discrete data and want to calculate the percentiles and the 
 > percentile ranks for each of the unique scores. I can calculate the 
 > percentiles with quantile().
 > 
 > I know that "ecdf" can be used to calculate the empirical cumulative 
 > distribution. However, I don't know how to exact the cumulative 
 > probabilities for each unique element. The requirement is similar to the 
 > "FREQUENCIES" in SPSS. Could someone help me in exacting the cumulative 
 > probabilities from the ecdf object? Thanks in advance!

You can use the following function:

f.freq <- function(x)
{
  tab <- data.frame(table(x))
  tab$Percent <- tab$Freq*100/length(x)
  tab$Cum.Percent[1] <- tab$Percent[1]
  for(i in 2:length(tab[,1]))
    tab$Cum.Percent[i] <- tab$Cum.Percent[i-1] + tab$Percent[i]
  tab
}

x <- round( rnorm(50, mean=50, sd=10) )
f.freq(x)

This should give you a table analog to frequencies in SPSS.


 > 
 > # Generating artificial data
 > x <- round( rnorm(50, mean=50, sd=10) )
 > probs <- seq(0.1, 0.9, by=0.1)
 > # Calculating percentiles
 > x.percentiles <- quantile(x, probs)
 > # Calculating percentile ranks
 > x.ranks <- ecdf(x)
 > 
 > Best,
 > Mike
 > 
 > ______________________________________________
 > R-help at stat.math.ethz.ch mailing list
 > https://stat.ethz.ch/mailman/listinfo/r-help
 > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Best,
Christoph

-- 
Christoph Buser <buser at stat.math.ethz.ch>
Seminar fuer Statistik, LEO C11
ETH (Federal Inst. Technology)	8092 Zurich	 SWITZERLAND
phone: x-41-1-632-5414		fax: 632-1228
http://stat.ethz.ch/~buser/




More information about the R-help mailing list