我搜索最好的方式(我没有找到当前的api,但也许我错了)为scala集合计算不同类型的排名,如IndexedSeq(如R中的这种不同策略:http://stat.ethz.ch/R-manual/R-devel/library/base/html/rank.html)< / p>
val tabToRank = IndexedSeq(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5)
例如,“第一级战略”等于第一次出现胜利,返回
tabToRank.rank("first")
# return (4,1,6,2,7,11,3,10,8,5,9)
例如,我有这样一个研究案例:如果你有最终状态模拟的人口城市列表(像tabToRank
这样的矢量数据),我需要a)排名和b)排序城市按排名来绘制像“按人口划分的城市等级”的图形等于众所周知的等级大小分布(src of img):
答案 0 :(得分:2)
对于城市数据,您需要
citipop.sortBy(x => -x).zipWithIndex.map(_.swap)
首先对最大的种群进行排序(默认是最小的,然后我们对负数进行排序),然后对它们进行编号,然后得到数字,第二个数字。
但是,Scala没有内置的统计库。通常,您必须知道自己想要做什么,自己动手做或使用Java库(例如Apache Commons Math)。答案 1 :(得分:1)
这是一段代码,它以你给出的例子为例:
object Rank extends App {
val tabToRank = IndexedSeq(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5);
def rank[A](input: Seq[A])(implicit ord: Ordering[A]): Seq[Int] = {
// transform to a pair of value/index
val withIndices: Seq[(A,Int)] = input.zipWithIndex;
// sort by the values
val sorted: Seq[(A,Int)] = withIndices.sortBy(_._1);
// keep only the indices
val indices = sorted.map(_._2);
// create the inverse permutation
val r = new collection.mutable.ArraySeq[Int](indices.size);
for((i,j) <- indices.zipWithIndex)
r(i) = j;
return r;
}
println(rank(tabToRank));
}
有:
(请注意,它从0开始计数而不是1,因为基本上所有编程语言都是如此。)
我不理解将其包含在其中的其他内容(策略)。