首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Rcpp函数后Rcpp崩溃

使用Rcpp函数后Rcpp崩溃
EN

Stack Overflow用户
提问于 2015-07-15 18:26:35
回答 1查看 358关注 0票数 1

我要做的是找出多个大型矩阵之间的匹配日期。我希望我的C++代码返回的是行索引,其中找到了匹配项。

我对C++完全陌生,我发现加快我的R代码是非常有用的。

我的代码似乎在function中工作,但是在我的R代码中的另一个循环中通过一些迭代使用了Rcpp函数之后就崩溃了

以下是一些示例数据

baseflow_mat[1]是按如下格式格式化的矩阵

baseflow_mat[2]是我想在哪里找到匹配的一个例子。

代码语言:javascript
复制
baseflow_mat<-list()

baseflow_mat[[1]]<-data.frame(year=c(1992,1992,1992,1992),month=c(7,7,7,7),day=c(5,10,13,17),flow=c(50,60,59,33))

baseflow_mat[[2]]<-data.frame(year=c(1992,1992,1992,1992,1992,1992,1992,1992),month=c(7,7,7,7,7,7,8,8),day=c(4,10,13,18,26,27,2,6),flow=c(50,60,59,33,45,40,55,52))

我想在所有170个大型baseflow_mat矩阵中找到匹配日期

baseflow_mat[2]是我想在哪里找到匹配的一个例子。

所以,我希望我的C++代码返回来自baseflow_mat_1的所有匹配项的行索引,这对于小矩阵很好,但是一旦我开始使用我的所有数据,它就会崩溃。我的实际数据在每个矩阵中有500到3000行,我希望为170个单独的矩阵找到匹配,所以170个*170个,大约有28900个结果。

这是我的R码

代码语言:javascript
复制
library(Rcpp)
sourceCpp("Source1.cpp")

big_match<-list()
for(i in seq(1,2)){#length(baseflow_mat))){
  match_baseflow_list<-list()
  for(j in seq(1,2)){#length(baseflow_mat))){
     matches_wzeros<-matchRows(nrow(baseflow_mat[[j]][,1:3]),nrow(baseflow_mat[[i]][,1:3]),baseflow_mat[[j]][,1:3],baseflow_mat[[i]][,1:3])
     matches<-matches_wzeros[matches_wzeros>0]
     match_baseflow_list[[j]]<-baseflow_mat[[j]][matches,]
   }
  big_match[[i]]<-match_baseflow_list
}

这是我的C++代码

代码语言:javascript
复制
// [[Rcpp::export]]

NumericVector matchRows(int rowSize, int matchRowSize, DataFrame nonMatchDF, DataFrame matchDF)
{
//0 is for year, 1 is for month, 2 is for day for both DF
Rcpp::NumericVector nonmatchYear = nonMatchDF[0];
Rcpp::NumericVector nonmatchMonth = nonMatchDF[1];
Rcpp::NumericVector nonmatchDay = nonMatchDF[2];

Rcpp::NumericVector matchYear = matchDF[0];
Rcpp::NumericVector matchMonth = matchDF[1];
Rcpp::NumericVector matchDay = matchDF[2];

Rcpp::NumericVector indexMatrix(matchRowSize*rowSize);


//j is for going through the nonmatch dataframe
int j;

//i is for going through the other DF
int i;

//addIndex is to add correctly to the vector
int addIndex = 0;

for (i = 0; i < matchRowSize; i++)
{
    //Rcpp::NumericVector indexMatch(rowSize);


    for (j = 0; j < rowSize; j++)
    {

        if ((matchYear[i] == nonmatchYear[j]) && (matchMonth[i] == nonmatchMonth[j]) && (matchDay[i] == nonmatchDay[j]))
        {
            indexMatrix[addIndex + j] = j + 1 ;
            //indexMatrix(i, j) = j+1;
        }
    }

    addIndex = addIndex + (j+1);
}

return indexMatrix;
  }

和sessionInfo()输出

代码语言:javascript
复制
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils    
[5] datasets  methods   base     

other attached packages:
[1] Rcpp_0.11.6

loaded via a namespace (and not attached):
[1] tools_3.1.1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-20 04:58:36

您在最内部循环中所做的本质上是一个merge。如果需要更快的速度,请使用dplyr::left_join (merge的快速替代)。

代码语言:javascript
复制
big_match<-list()
for(i in seq(1,2)){#length(baseflow_mat))){
  match_baseflow_list<-list()
  for(j in seq(1,2)){#length(baseflow_mat))){
    match_baseflow_list[[j]] <- merge(baseflow_mat[[1]], baseflow_mat[[2]], by=c("year", "month", "day"))
  }
  big_match[[i]]<-match_baseflow_list
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31438251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档