繁体   English   中英

如何对具有 NA 值的向量进行排序,并将 NA 值放在 Rcpp 或 C++ 中的结果末尾?

[英]How do I sort a vector with NA values, and place the NA values at the end of the result in Rcpp or C++?

我正在尝试使用 C++ 通过 Rcpp ZEFE90A8E604A67C6D4 优化 R 中的排序 function。 我试图模仿的 function 是sort(x, decreasing = TRUE, na.last = TRUE) 我已经能够使用以下代码实现第一个参数decreasing = TRUE就好了:

library(Rcpp)
x <- c(3, 7, 21, 6, NA, 1, 8, 7)


# base R sorting
sort(x, decreasing = TRUE, na.last = TRUE)
# [1] 21  8  7  7  6  3  1 NA

# Simple Rcpp sorting function
cppFunction("NumericVector sort_cpp(NumericVector x, bool decreasing = false) {
  NumericVector sorted = clone(x).sort(decreasing);
  return sorted;
}")
sort_cpp(x, decreasing = TRUE)
# [1] NA 21  8  7  7  6  3  1

我对 C++ 或 Rcpp package 不够精通,无法在所有情况下将 NA 值放在最后。 当我将decreasing设置为 FALSE 时,默认情况下会将 NA 值放在末尾,但是当它反转(设置为 TRUE)时,它会翻转整个向量,并将 NA 值 go 放在向量的前面。 我已经尝试了一些成功的事情(同样,C++ 中的新手),并且能够在 C++ shell 中运行一些东西,但我无法成功地将其编码为 ZE1E1Z0D4805732837E6 这主要是从这个问题中复制和粘贴的:

#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

int main()
{
int n, k = 4, j; // k is number of elements
double x = -0.0;
double i = 0;
double swap = 0;//used in the function as a place holder and used for swapping between other variables
double a[100] = { (1/x) + (1/i), 2.3, 1/x *0, 1/i };//array of double elements // 1/i * 0 is NaN
//(1 / i) * 0


for (n = 0; n < (k - 1); n++) // for loop consists of variables and statements in order to arrange contents of array
{
  for (j = 0; j < k - n - 1; j++)
    {
      if (!std::isnan(a[j + 1]) && std::isnan(a[j]) || (a[j] > a[j + 1]))
        {
        swap = a[j];
        a[j] = a[j + 1];
        a[j + 1] = swap;

        }
    }
}

cout << "The list of sorted elements within the array, is: " << endl; /* Output message to user */
for (int i = 0; i < k; i++)// Loop up to number of elements within the array
{
    cout << a[i] << " ";/* Output contents of array */
}
cout << endl; //new line

return 0; 
}

有没有办法使用 Rcpp package 或等价物将其放入 R 中?

我在卡巴的建议的帮助下想通了。 它只需要更改用于确定排序顺序的符号。 顺便说一下,这是我能够在 Rcpp 中用来生成 R 等效排序算法的内容。 与基础 R 相比,我尚未对其进行测试以确定性能,因此可能有更好的方法来获得相同的结果,首先。 保存要获取的 a:cpp 文件:

// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace Rcpp;
using namespace std;

// [[Rcpp::export]]
NumericVector sort_cpp(NumericVector x, bool decreasing = false, bool nalast = false) {
  NumericVector y = clone(x);
  if(nalast) {
    if(decreasing) {
      std::sort(std::begin(y), std::end(y),
                [](double d0, double d1) {
                  if( isnan(d0) ) return false;
                  if( isnan(d1) ) return true;
                  return d0 > d1;
                });
    } else {
      std::sort(std::begin(y), std::end(y),
                [](double d0, double d1) {
                  if( isnan(d0) ) return false;
                  if( isnan(d1) ) return true;
                  return d0 < d1;
                });
    }
  } else {
    y.sort(decreasing);
  }
  return y;
}

然后使用Rcpp::sourceCpp()编译 function 使用。 我没有合并 na.last = FALSE 的情况,其中 NA 值将被删除。 这超出了我使用的 scope 的范围,但是如果将来偶然发现这个问题,这应该可以帮助其他人入门!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM