簡體   English   中英

使用Rcpp函數后R Studio崩潰

[英]R Studio crashes after using Rcpp function

我想做的是找到多個大型矩陣之間的匹配日期。 我希望我的C ++代碼返回的是找到匹配項的行索引

我對C ++完全陌生,我發現它對加速我的R代碼非常有用。

我的代碼似乎可以在R Studio中工作,但是在我的R代碼的另一個循環中通過一些迭代使用了Rcpp函數后,崩潰了

這是一些示例數據

baseflow_mat [[1]]是一個格式如下的矩陣

baseflow_mat [[2]]是我要查找匹配項的示例

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))

我想在baseflow_mat的所有170個大型矩陣中找到匹配的日期

baseflow_mat [[2]]是我要查找匹配項的示例

因此,我希望我的C ++代碼返回的是baseflow_mat_1中所有匹配項的baseflow_mat_2的行索引。 這適用於小型矩陣,但是一旦我開始使用所有數據,它就會崩潰。 我的實際數據在每個矩陣中都有500到3000行,我想找到170個單獨矩陣的匹配項,因此170 * 170約28900個結果

這是我的R代碼

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 ++代碼

// [[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()輸出

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

您在最里面的循環中執行的操作本質上是merge 如果需要更快,請使用dplyr::left_joinmerge的快速替代方法)。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM