[Rcpp-devel] Doubt about best pratices

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Apr 30 19:26:17 CEST 2022


A data frame is designed to represent different types in different columns. A matrix is designed so that all elements are the same.

It sounds like you need matrices for your algorithm. I would simply require that... or attempt to convert it immediately and stop if the result is not numeric. 

On April 30, 2022 10:16:04 AM PDT, Roberto Spadim <roberto at spadim.com.br> wrote:
>Hi folks!
>
>What's the best pratices to accept a dataframe and numericmatrix in a function?
>
>Should I have two different methods to deal with NumericMatrix and DataFrame?
>Could I convert DataFrame to matrix? What's the impact of doing this,
>i.e. does the as.matrix copy dataframe data?
>
>Thanks in advance!
>
>---
>Today I'm doing something like this:
>
>void RCPP_function(const RObject &vec, const RObject &mat) {
>  if(mat.isNULL())
>    Rcpp::stop("mat can't be null");
>  NumericMatrix variable_matrix;
>  if (is<NumericMatrix>(mat))
>    variable_matrix = as<NumericMatrix>(mat);
>  else
>    variable_matrix = Rcpp::internal::convert_using_rfunction(mat, "as.matrix");
>
>  NumericVector variable_vector;
>  if((is<NumericVector>(target) || is<DataFrame>(target)) && !target.isNULL()){
>      variable_vector=as<NumericVector>(target);
>  }else{
>      variable_vector=colMeans(variable_matrix);
>  }
>
>  ...
>  do something with data
>  but here it's sure to be:
>   NumericVector (variable_vector)
>   NumericMatrix (variable_matrix)
>  ...
>}
>
>
>-- 
>Roberto Spadim
>_______________________________________________
>Rcpp-devel mailing list
>Rcpp-devel at lists.r-forge.r-project.org
>https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
Sent from my phone. Please excuse my brevity.


More information about the Rcpp-devel mailing list