小编SEA*_*yst的帖子

当我期望 R 中的数据帧输出时,为什么 Rccp 返回类似列表的输出?

我正在尝试编写一个 .cpp,它接受一个输入向量并输出一个包含输入向量中所有可能组合的两列数据帧。我的输出给出了所需的值,但不是作为数据帧。我在 .cpp 文件中更改了什么才能获得数据帧输出?

我的possible_combos.cpp文件如下所示:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
GenericVector C_all_combos(GenericVector a) {
  int vec_length = a.size();
  int vec_length_sq = vec_length*vec_length; 
  GenericVector expand_vector_a(vec_length_sq);
  GenericVector expand_vector_b(vec_length_sq);  
  for (int i=0; i<vec_length_sq; i++) { expand_vector_a[i] = a[i / vec_length]; };
  for (int i=0; i<vec_length_sq; i++) { expand_vector_b[i] = a[i % vec_length]; };
  DataFrame my_df = DataFrame::create(Named("v_1") = expand_vector_a,
                                    Named("v_2") = expand_vector_b);
return my_df;
}

/*** R
C_all_combos(c(1, "Cars", 2.3))
  */
Run Code Online (Sandbox Code Playgroud)

运行所需的输出Rcpp::sourceCpp("possible_combos.cpp")是:

    v_1    v_2
    1       1
    1 …
Run Code Online (Sandbox Code Playgroud)

c++ r rcpp

5
推荐指数
2
解决办法
95
查看次数

标签 统计

c++ ×1

r ×1

rcpp ×1