是rank,而不是order

最近在看GSEA的1.0源码,对order仔细的看了一下,觉得应该写下来。
order的官方说它返回一个能将参数排序的permutation。
order的行为是:(A)返回新有序列对应位置的元素在原序列中的索引号,也就
是位置;有些情况下我们会认为order(B)返回原序列对应位置的元素在新序列
中的位置:秩(rank),这是不对的,两者不等价。
例如
> a<-sample(seq(1:10),5)
> a
[
11 4 9 8 3
> a.order<-order(a)
> a.order  
[1] 1 5 2 4 3   #可以看到返回值是(A)所述的情况,(B)所述的情况为 1 3 5 4 2
(B)所述的情况可以用下面的两种操作得到
> a.sort<-sort(a)
> match(a,a.sort)
[
11 3 5 4 2
> rank(a)
[1] 1 3 5 4 2
同时order函数等价于
> match(a.sort,a)
[1] 1 5 2 4 3
不要搞混
有趣的是:1,不论一个vector A有序还是无序,
               执行A[order(A)]将得到sort(A)的结果。
               也就是说,在操作上,vec[order(vec)]与
               sort(vec)等价。
            2,order运算性质:
               order(a) ∈(1,length(a))
               奇数次order运算的结果相同,偶数次order运算结果相同
> a<-sample(seq(1:10),5,replace=F)
> sort(a)
[
11 3 4 5 8
> a[order(a)]
[
11 3 4 5 8
 order(a)
[1] 4 1 2 5 3
> order(order(a))
[1] 2 3 5 1 4
> order(order(order(a)))
[1] 4 1 2 5 3
> order(order(order(order(a))))
[1] 2 3 5 1 4

总结:
order <==> match(a.sort,a)
rank  <==> match(a,a.sort)

posted on 2011-12-08 11:34 ewre 阅读(201) 评论(0)  编辑 收藏 引用 所属分类: R


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2011年12月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

留言簿(2)

文章分类

文章档案

最新评论

阅读排行榜