diff --git a/NEWS.md b/NEWS.md index 7a6e93aa02..95d5131a8d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # dplyr 0.4.3.9000 +* `tbl_df` automatically generates column names (#1606). + * `mutate` failed to deep copy data that ends up in a list column (#1643). * `mutate` handles adding a factor that is all `NA` (#1645). diff --git a/src/matrixToDataFrame.cpp b/src/matrixToDataFrame.cpp index 6008f788f8..4da4748b80 100644 --- a/src/matrixToDataFrame.cpp +++ b/src/matrixToDataFrame.cpp @@ -1,10 +1,7 @@ #include using namespace Rcpp; -// [[Rcpp::export]] -List matrixToDataFrame(RObject x) { - SEXPTYPE type = TYPEOF(x); - +IntegerVector get_dim( const RObject& x){ if (!x.hasAttribute("dim")) stop("`x` is not a matrix"); @@ -12,6 +9,33 @@ List matrixToDataFrame(RObject x) { if (dim.size() != 2) stop("`x` is not a matrix"); + return dim ; +} + +CharacterVector get_names( const RObject& x, const IntegerVector& dim){ + int nc = dim[1] ; + if( x.hasAttribute("dimnames") ){ + List dimnames = x.attr("dimnames") ; + try { + CharacterVector res( dimnames[1] ) ; + return res ; + } catch(...){} + } + + CharacterVector names( nc ) ; + for( int i=0; i