SHARE
TWEET

Untitled

a guest Oct 15th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Rcpp.h>
  2. using namespace Rcpp;
  3.  
  4. // [[Rcpp::export(rng = false)]]
  5. List df2list(const DataFrame& x) {
  6.   std::size_t nrows = x.rows();
  7.   std::size_t ncols = x.cols();
  8.   CharacterVector nms = x.names();
  9.   List res(no_init(nrows));
  10.   for (std::size_t i = 0; i < nrows; ++i) {
  11.     List tmp(no_init(ncols));
  12.     tmp.names() = nms;
  13.     for (std::size_t j = 0; j < ncols; ++j) {
  14.       switch(TYPEOF(x[j])) {
  15.       case LGLSXP:
  16.       case INTSXP: {
  17.         tmp[j] = as<LogicalVector>(x[j])[i];
  18.         break;
  19.       }
  20.       case REALSXP: {
  21.         tmp[j] = as<NumericVector>(x[j])[i];
  22.         break;
  23.       }
  24.       case STRSXP: {
  25.         tmp[j] = as<std::string>(as<CharacterVector>(x[j])[i]);
  26.         break;
  27.       }
  28.       case VECSXP: {
  29.         tmp[j] = as<List>(x[j])[i];
  30.         break;
  31.       }
  32.       default: stop("Unsupported type '%s'.", type2name(x));
  33.       }
  34.     }
  35.     res[i] = tmp;
  36.   }
  37.   return res;
  38. }
  39.  
  40. template <typename T>
  41. void cp(List& l, const T& v, std::size_t n, std::size_t idx) {
  42.   for (std::size_t i = 0; i < n; ++i) {
  43.     List tmp = as<List>(l[i]);
  44.     tmp[idx] = v[i];
  45.   }
  46. }
  47.  
  48. // [[Rcpp::export(rng = false)]]
  49. List df2list2(const DataFrame& x) {
  50.   std::size_t nrows = x.rows();
  51.   std::size_t ncols = x.cols();
  52.   CharacterVector nms = x.names();
  53.   List res(no_init(nrows));
  54.  
  55.   for (std::size_t i = 0; i < nrows; ++i) {
  56.     List l(no_init(ncols));
  57.     l.names() = nms;
  58.     res[i] = l;
  59.   }
  60.  
  61.   for (std::size_t j = 0; j < ncols; ++j) {
  62.     switch(TYPEOF(x[j])) {
  63.     case LGLSXP:
  64.     case INTSXP: {
  65.       cp(res, as<IntegerVector>(x[j]), nrows, j);
  66.       break;
  67.     }
  68.     case REALSXP: {
  69.       cp(res, as<NumericVector>(x[j]), nrows, j);
  70.       break;
  71.     }
  72.     case STRSXP: {
  73.       cp(res, as<std::vector<std::string>>(x[j]), nrows, j);
  74.       break;
  75.     }
  76.     case VECSXP: {
  77.       cp(res, as<List>(x[j]), nrows, j);
  78.       break;
  79.     }
  80.     default: stop("Unsupported type '%s'.", type2name(x));
  81.     }
  82.   }
  83.   return res;
  84. }
  85.  
  86.  
  87. /***R
  88. data("Batting", package = "Lahman")
  89. x <- Batting[1:10000, 1:10]
  90. # str(df2list2(x))
  91. # df2list2(iris)
  92.  
  93. bench::mark(
  94.   df2list(x),
  95.   df2list2(x),
  96.   purrr::transpose(x)
  97. )
  98. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top