Search code examples
c++rrcpp

Why does Rccp return a list-like output when I was expecting a dataframe output in R?


I am trying to write a .cpp that takes an input vector and outputs a two-column dataframe with all possible combinations from the input vector. My output gives the desired values, but not as a dataframe. What do I change in the .cpp file to get a dataframe output?

My possible_combos.cpp file looks like this:

#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))
  */

The desired output from running Rcpp::sourceCpp("possible_combos.cpp") is:

    v_1    v_2
    1       1
    1       Cars
    1       2.3
    Cars    1
    Cars    Cars
    Cars    2.3
    2.3     1
    2.3     Cars
    2.3     2.3

But what I get is:

    v_1..1. v_1..1..1 v_1..1..2 v_1..Cars. v_1..Cars..1 v_1..Cars..2 v_1..2.3. v_1..2.3..1 v_1..2.3..2
1       1         1         1       Cars         Cars         Cars       2.3         2.3         2.3
  v_2..1. v_2..Cars. v_2..2.3. v_2..1..1 v_2..Cars..1 v_2..2.3..1 v_2..1..2 v_2..Cars..2 v_2..2.3..2
1       1       Cars       2.3         1         Cars         2.3         1         Cars         2.3

Thanks for any tips! I'm familiar with excellent R functions like expand.grid(), but want to experiment with alternatives.


Solution

  • As the other answer stated, a GenericVector is a List and you cannot create a DataFrame with List columns using the Rcpp DataFrame constructor. You can however create a List and convert it to a data.frame manually, returning it as SEXP:

    #include <Rcpp.h>
    using namespace Rcpp;
    
    // [[Rcpp::export]]
    SEXP 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]; };
    
      List my_df = List::create(Named("v_1") = expand_vector_a,
                                Named("v_2") = expand_vector_b);
    
    
      my_df.attr("class") = "data.frame";
      my_df.attr("row.names") = Rcpp::seq(1, vec_length_sq);
    
      return my_df;
    }
    
    /*** R
    C_all_combos(c(1, "Cars", 2.3))
    */