Skip to content

Instantly share code, notes, and snippets.

@bmoska
Last active January 5, 2018 15:33
  • 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.R
library(foreach)
library(doParallel)
library(arules)
library(Rcpp)
library(isSub) #this is our Rcpp library
#register cores
registerDoParallel(3)
#function searching for a subsets
issub=function(data){
issub=rep(0,nrow(data))
Antecedent=data$lhs
Consequent=data$rhs
length=nrow(data)
for(i in 1:length){
for(j in (i+1):length){
if(isTRUE(grepl(Antecedent[j], Antecedent[i]))){
issub[i]=1
}
else if(isTRUE(grepl(Antecedent[i], Antecedent[j]))){
issub[j]=1
}
}
}
data$isSubset=issub
return(data)
}
#change datatype of rhs and lhs to character, removing '{}'
data$lhs=as.character(data$lhs)
data$rhs=as.character(data$rhs)
data$lhs=gsub('[{}]',replace='',data$lhs)
data$rhs=gsub('[{}]',replace='',data$rhs)
#create table of unique consequents-used for parallel computation with foreach
listOfUniqueConsequents=table(data$rhs)
#here calculation are performed sequentially, after division of the dataset into subgroups of unique consequents
resultsSequentiallyR=c()
resultsSequentiallyRcpp=c()
for(i in 1:length(listOfUniqueConsequents)){
subset=data[which(data$rhs==as.character(as.integer(names(listOfUniqueConsequents[i])))),]
resultsSequentiallyR=c(resultsSequentiallyR,issub(subset))
resultsSequentiallyRcpp=c(resultsSequentiallyRcpp,isSubset(subset))
}
#foreach with Rcpp function-remember to include package, otherwise function would not be exported
resultsParallelRcpp=foreach(k=1:length(listOfUniqueConsequents),.packages=c("isSub"))%dopar%{isSubset(data[which(data$rhs==as.character(as.integer(names(listOfUniqueConsequents[k])))),])}
#foreach with R function
resultsParallelR=foreach(k=1:length(listOfUniqueConsequents))%dopar%{issub(data[which(data$rhs==as.character(as.integer(names(listOfUniqueConsequents[k])))),])}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment