Create a gist now

Instantly share code, notes, and snippets.

@bmoska /isSubset.cpp Secret
Last active Feb 8, 2018

Embed
What would you like to do?
isSubset.cpp
#include <Rcpp.h>
#include "string"
using namespace Rcpp;
// [[Rcpp::export]]
DataFrame isSubset(DataFrame data) {
int n=data.nrows();
StringVector lhs;
StringVector rhs;
try{
StringVector lhs=data["lhs"];
StringVector rhs=data["rhs"];
}
catch(std::exception e){
Rcpp::Rcout<<"Invalid column names were passed, function returns empty DataFrame" <<std::endl;
};
std::string *arrayLhs=new std::string [n];
std::string *arrayRhs=new std::string [n];
for(int i=0;i<n;i++){
arrayLhs[i]=Rcpp::as<std::string> (lhs(i));
arrayRhs[i]=Rcpp::as<std::string> (rhs(i));
}
NumericVector hasSubset=NumericVector(n);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if((arrayLhs[i].find(arrayLhs[j])!= std::string::npos) & arrayRhs[i]==arrayRhs[j]){
hasSubset[i]=1;
}
else if((arrayLhs[j].find(arrayLhs[i])!= std::string::npos) & arrayRhs[i]==arrayRhs[j]){
hasSubset[j]=1;
}
}
}
delete [] arrayLhs;
delete [] arrayRhs;
data["hasSubset"]=hasSubset;
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment