我一直在尝试使用Rcpp在c ++中获得向量的等级.我使用过其他糖功能
is_na();
Run Code Online (Sandbox Code Playgroud)
c ++中的等级R函数是否有类似的糖函数.还有Rcpp中可用的R糖功能列表/
我有一个带有双精度的向量,我想对其进行排名(实际上它是一个带有带有名为 的双精度成员的对象的向量costs)。如果只有唯一值或者我忽略非唯一值,那么就没有问题。但是,我想对非唯一值使用平均排名。此外,我在 SO 上发现了一些关于排名的问题,但是他们忽略了非唯一值。
例如,假设我们有 (1, 5, 4, 5, 5),那么相应的排名应该是 (1, 4, 2, 4, 4)。当我们忽略非唯一值时,排名为 (1, 3, 2, 4, 5)。
当忽略非唯一值时,我使用了以下内容:
void Population::create_ranks_costs(vector<Solution> &pop)
{
size_t const n = pop.size();
// Create an index vector
vector<size_t> index(n);
iota(begin(index), end(index), 0);
sort(begin(index), end(index),
[&pop] (size_t idx, size_t idy) {
return pop[idx].costs() < pop[idy].costs();
});
// Store the result in the corresponding solutions
for (size_t idx = 0; idx < n; ++idx)
pop[index[idx]].set_rank_costs(idx + 1);
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何考虑非唯一值?我更喜欢使用,std::algorithm因为在我看来,这会带来干净的代码。