Using the information from @baptiste's answer , this is what ultimately gives a well-formed data frame:
RcppExport SEXP makeDataFrame(SEXP in) { Rcpp::DataFrame dfin(in); Rcpp::DataFrame dfout; Rcpp::CharacterVector namevec; std::string namestem = "Column Heading "; for (int i=0;i<2;i++) { dfout.push_back(dfin(i)); namevec.push_back(namestem+std::string(1,(char)(((int)'a') + i))); } dfout.attr("names") = namevec; Rcpp::DataFrame x; Rcpp::Language call("as.data.frame",dfout); x = call.eval(); return x; }
I think it remains as before that this might be inefficient due to push_back (as suggested by @Dirk) and a second language score. I looked at rcpp unitTests and still could not come up with something better. Does anyone have any idea?
Update:
Using @Dirk's suggestions (thanks!), This seems to be a simpler and more efficient solution:
RcppExport SEXP makeDataFrame(SEXP in) { Rcpp::DataFrame dfin(in); Rcpp::List myList(dfin.length()); Rcpp::CharacterVector namevec; std::string namestem = "Column Heading "; for (int i=0;i<dfin.length();i++) { myList[i] = dfin(i); // adding vectors namevec.push_back(namestem+std::string(1,(char)(((int)'a') + i))); // making up column names } myList.attr("names") = namevec; Rcpp::DataFrame dfout(myList); return dfout; }
highBandWidth
source share