Skip to content

Instantly share code, notes, and snippets.

@bmoska
Last active February 8, 2018 15:29
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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