R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # ranknfile.r November 2007, 2008 > # > # demo of sort, etc > # > # generate some easy data > set.seed(1917515) > N <- 12 > U <- floor(1000*runif(N)) > U [1] 498 623 103 734 505 75 667 173 168 130 201 243 > # > # sort the data -- the order statistics > sU <- sort(U) # sU sorted values > sU [1] 75 103 130 168 173 201 243 498 505 623 667 734 > # > # get the antiranks > oU <- order(U) # oU antiranks > oU [1] 6 3 10 9 8 11 12 1 5 2 7 4 > # > # order can get you the order statistics by > U[oU] [1] 75 103 130 168 173 201 243 498 505 623 667 734 > sum( U[oU]==sU ) # should be same as sU [1] 12 > # > # to get the ranks, invert this permutation > rU <- order(oU) > rU [1] 8 10 2 12 9 1 11 5 4 3 6 7 > # > # in one step, this is > ranks <- order(order(U)) > ranks [1] 8 10 2 12 9 1 11 5 4 3 6 7 > # > # or, to use something specially designed > rank(U) [1] 8 10 2 12 9 1 11 5 4 3 6 7 > # > # find where other values are among these > # for given x, find i for sorted list v such that > # v[i] le x lt v[i+1], v[0]=-inf, v[N+1]=+inf > W <- c( 50, 135, 173, 500, 808) > findInterval(W,sU) # default rightmost.closed=FALSE [1] 0 3 5 8 12 > # done > rm(list=ls()) > q()