1

I want to use Rcpp to make certain parts of my code more efficient. I have a main R function in which my objects are defined, in this R functions I have several rcpp functions that use the r data objects as inputs. This is one example of an rcpp function that is called in the R-function:

void calculateClusters ( List order, 
                                 NumericVector maxorder,
                                 List rank,
                                 double lambda, 
                                 int nbrClass, 
                                 NumericVector nbrExamples) {

  int current, i;

  for ( current = 0; current < nbrClass; current ++ ) {
    maxorder [current] = 0;

    for ( i = 0; i < nbrExamples [ current ]; i++ ) {
        order[ current ][i] = ( int ) ( rank[ current ][i] / lambda ) - 1;
    }

      if ( order[ current ][i] > maxorder[ current ] ) {
        maxorder[ current ] = order[ current ][i];
      }
    }
  }

This function calculates the maximum number of clusters for each class. In native c++ coding I would define my List as an int** and my NumericVector as int*. However in Rcpp this gives an error. I know the fault lies in the subsetting of these Lists (I handled them the same way as int**).

My question is how can I transform these int** succesfully into List, without loosing flexibility. For example the List order and distance have the structure order[[1]][1:500], order[[2]][1:500], so this would be exactly the same as int** in c++ where it would be order[1][1:500], order[2][1:500]. If there are 3 classes the order and the distance List change to order order[[1]][1:500], order[[2]][1:500], order[[3]][1:500]. How can I do this in Rcpp?

  • 1
    Here is an example of handling lists: http://stackoverflow.com/a/12734655/1412059 – Roland Jan 28 '16 at 09:20
  • What error are you getting? You should be able to subset the `List` the way you’ve described, except that you need to tell C++ that the type of `rank[current]` is *also* a subsettable element (list? vector?). – Konrad Rudolph Jan 28 '16 at 15:02

1 Answers1

3

Briefly:

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725