[英]How to access lists in Rcpp?
I want to use Rcpp to make certain parts of my code more efficient. 我想使用Rcpp来提高代码的某些部分的效率。 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. 我有一个主要的R函数,用于定义我的对象,在这个R函数中,我有几个rcpp函数,这些函数使用r数据对象作为输入。 This is one example of an rcpp function that is called in the R-function: 这是在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];
}
}
}
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*
. 在本机c ++编码中,我将List
定义为int**
,将NumericVector
为int*
。 However in Rcpp this gives an error. 但是,在Rcpp中,这会导致错误。 I know the fault lies in the subsetting of these Lists
(I handled them the same way as int**
). 我知道错误在于这些Lists
的子集(我以与int**
相同的方式处理它们)。
My question is how can I transform these int**
succesfully into List
, without loosing flexibility. 我的问题是如何在不失去灵活性的情况下将这些int**
成功转换为List
。 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]
. 例如, List order
和distance
的结构order[[1]][1:500], order[[2]][1:500]
,因此这与c ++中的int**
完全相同是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]
. 如果有3类,则order
和distance
List
将更改为订单order[[1]][1:500], order[[2]][1:500], order[[3]][1:500]
。 How can I do this in Rcpp? 如何在Rcpp中做到这一点?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.