Spearman的脚掌距离(以R为底) [英] Spearman's footrule distance with base R

查看:119
本文介绍了Spearman的脚掌距离(以R为底)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出两个排列:

v1
[1] 4 3 1 5 2

v2
[1] 2 3 4 5 1

如何计算基数为R的Spearman的尺距(所有元素的总位移)? (可灵活更改大小为n的任意两个)

How do you compute the Spearman's footrule distance (total displacement of all elements) with base R? (flexible for any two permutations of size n)

例如,对于这两个向量,如下所示:

For example, for these two vectors, it's as follows:

1v1移到v2
2个位置 2v1移到v2
4个位置 3v1移到v2
0个位置 4v1移到v2
2个位置 5v1移到v2

1 is moved 2 places from v1 to v2
2 is moved 4 places from v1 to v2
3 is moved 0 places from v1 to v2
4 is moved 2 places from v1 to v2
5 is moved 0 places from v1 to v2

总距离为:2+4+0+2+0 = 8

推荐答案

以下是使用sapplywhichsum的方法:

Here is a method using sapply, which, and sum:

sum(sapply(seq_along(v1), function(i) abs(i - (which(v2 == v1[i])))))

在这里,我们沿v1的索引移动,并计算当前索引中元素的索引距其在v2中的位置的距离.然后将它们汇总在一起.

Here, we move along the indices of v1 and calculate the distance of the index of the element in the current index from its position in v2. These are then summed together.

我怀疑评论中类似@alexis_laz的解决方案可能会提高计算效率.

I suspect something along the lines of @alexis_laz's solution in the comments may have greater computational efficiency.

这篇关于Spearman的脚掌距离(以R为底)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆