【问题标题】:R Studio crashes after using Rcpp functionR Studio 在使用 Rcpp 函数后崩溃
【发布时间】:2015-10-04 22:58:08
【问题描述】:

我要做的是在多个大矩阵之间找到匹配的日期。我希望我的 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

【问题讨论】:

  • 请包含导致崩溃的数据和 R 代码。
  • 我想你还是误解了reproducible example 是什么。
  • 很抱歉。我已经更新了在 R Studio 中重现崩溃的代码
  • 你在做merge吗? merge(baseflow_mat[[1]], baseflow_mat[[2]], by=c("year", "month", "day"))
  • 我完全忽略了使用合并。这正是我想要的,非常感谢。

标签: c++ r memory match rcpp


【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多