如何访问Rcpp中的列表?

时间:2016-01-28 09:03:01

标签: c++ r rcpp

我想使用Rcpp使我的代码的某些部分更有效。我有一个主要的R函数,我的对象被定义,在这个R函数中我有几个rcpp函数,它们使用r数据对象作为输入。这是在R函数中调用的rcpp函数的一个示例:

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];
      }
    }
  }

此函数计算每个类的最大簇数。在本机c ++编码中,我将List定义为int**,将NumericVector定义为int*。但是在Rcpp中,这会产生错误。我知道错误在于这些Lists的子集(我用与int**相同的方式处理它们)。

我的问题是如何在不失去灵活性的情况下,将这些int**成功转换为List。例如,List orderdistance具有结构order[[1]][1:500], order[[2]][1:500],因此这与c ++中的int**完全相同,其中order[1][1:500], order[2][1:500]order。如果有3个类,则distanceList order[[1]][1:500], order[[2]][1:500], order[[3]][1:500]会更改为订单run.getCTR().addNewFldChar().setFldCharType(STFldCharType.???) 。我怎么能在Rcpp中做到这一点?

1 个答案:

答案 0 :(得分:1)