Rcpp 函数选择(并返回)子数据帧

标签 r rcpp

是否可以编写一个 C++ 函数来获取 R 数据帧作为输入,然后修改该数据帧(在我们的例子中采用子集)并返回新的数据帧(在这个问题中,返回一个子数据帧)? 我下面的代码可能会让我的问题更清楚:

代码:

# Suppose I have the data frame below created in R:
myDF = data.frame(id = rep(c(1,2), each = 5), alph = letters[1:10], mess = rnorm(10))

# Suppose I want to write a C++ function that gets id as inout and returns 
# a sub-dataframe corresponding to that id (**If it's possible to return 
# DataFrame in C++**)

# Auxiliary function --> helps get a sub vector:
arma::vec myVecSubset(arma::vec vecMain, arma::vec IDVec, int ID){
  arma::uvec AuxVec = find(IDVec == ID);
  arma::vec rslt = arma::vec(AuxVec.size());
  for (int i = 0; i < AuxVec.size(); i++){
    rslt[i] = vecMain[AuxVec[i]];
  }
  return rslt;
}

# Here is my C++ function:
Rcpp::DataFrame myVecSubset(Rcpp::DataFrame myDF, int ID){
  arma::vec id = Rcpp::as<arma::vec>(myDF["id"]);
  arma::vec alph = Rcpp::as<arma::vec>(myDF["alpha"]);
  arma::vec mess = Rcpp::as<arma::vec>(myDF["mess"]);

  // here I take a sub-vector:
  arma::vec id_sub = myVecSubset(id, id, int ID);
  arma::vec alph_sub = myVecSubset(alph, id, int ID);
  arma::vec mess_sub = myVecSubset(mess, id, int ID);

  // here is the CHALLENGE: How to combine these vectors into a new data frame???
  ???
}

总结起来,其实主要有两个问题: 1)有没有更好的方法在C++中获取上面的子数据帧? (希望我可以简单地说 myDF[myDF$id == ID,]!!!)

2)我是否可以将 id_sub、alpha_sub 和mess_sub 组合到 R 数据帧中并返回它?

非常感谢您的帮助。

最佳答案

要补充 Romain 的答案,您可以尝试调用 [通过 Rcpp 进行操作。如果我们了解如何 df[x, ]被评估(即,它实际上是对 "[.data.frame"(df, x, R_MissingArg) 的调用,这很容易做到:

#include <Rcpp.h>
using namespace Rcpp;

Function subset("[.data.frame");

// [[Rcpp::export]]
DataFrame subset_test(DataFrame x, IntegerVector y) {
  return subset(x, y, R_MissingArg);
}

/*** R
df <- data.frame(x=1:3, y=letters[1:3])
subset_test(df, c(1L, 2L))
*/

给我

> df <- data.frame(x=1:3, y=letters[1:3])
> subset_test(df, c(1L, 2L))
  x y
1 1 a
2 2 b

在 Rcpp 中,对 R 的回调通常会较慢,但根据瓶颈的程度,它对您来说仍然足够快。

但要小心,因为此函数将对整数向量使用基于 1 的子集而不是基于 0 的子集。

关于Rcpp 函数选择(并返回)子数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22828361/

相关文章:

regex - 如何在 R 中的字符串中搜索美元符号?

r - 尝试使用 Rticles 生成示例 R markdown : pdflatex not found Ubuntu 20. 04 时出错

c++ - 如何让 Armadillo 函数在反转奇异矩阵时不打印错误?

r - 如何处理两个列表中的NA?

r - 从不等长的列表中加入数据帧

c++ - 子集化后将包含索引号的列表映射到标准索引序列

c++ - 如何用另一个矩阵的值替换 C++ 中矩阵的元素(使用 Rcpp)?

c++ - 使用 Rcpp 开发现有的 R 包

r - 用 R 定义 Excel 的列宽

r - 将 lambda 向量传递给 Rcpp 的 rpois