Reputation: 60
What I'm trying to do is find matching dates between multiple large matrices. And what I want my C++ code to return is row indices where matches are found
I am completely new to C++ and I have found it extremely useful to speed up my R code.
My code seems to work in R Studio but crashes after the Rcpp function has been used through some iterations in another loop in my R code
Here is some example data
baseflow_mat[[1]] is a matrix formatted as so
baseflow_mat[[2]] is an example of where I want to find matches
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))
And I want to find matching dates across all 170 large matrices of baseflow_mat
baseflow_mat[[2]] is an example of where I want to find matches
So what I want my C++ code to return is the row indices of baseflow_mat_2 of all matches from baseflow_mat_1. This works fine with small matrices but once I start to use all my data it starts to crash. My actual data has anywhere from 500 to 3000 rows in each matrix and I want to find matches for 170 separate matrices so 170*170 about 28900 results
Here is my R code
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
}
Here is my C++ code
// [[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;
}
And sessionInfo() output
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
Upvotes: 1
Views: 371
Reputation: 4473
What you are doing inside the innermost loop is essentially a merge
. If you need it to be faster, use dplyr::left_join
(a fast alternative for merge
).
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
}
Upvotes: 4