diff --git a/a3.r b/a3.r new file mode 100644 index 0000000000000000000000000000000000000000..6b0367782b4fcab69af21d3a82d476d67ce06c96 --- /dev/null +++ b/a3.r @@ -0,0 +1,56 @@ +### + # R code for assignment. + ## + + +# Clear console. +cat('\014') + + +# install.packages('arules', dependencies=TRUE) +library(arules) + + +### + # The "cat" function seems to be an all around better alternative to "print". + # ("print" has a tendency to add extra, frivilous output that makes it look dirty and difficult to read.) + # + # This wrapper for "cat" then automatically adds a newline to the end of the message, effectively accomplishing + # what you would intuitively expect "print" to do. + # + # See references.md file for source of idea. + ## +catn <- function(message, trailing='\n') { + cat(message) + cat(trailing) +} + + +### + # Tests the build-in R "apriori" function using the in class example from the powerpoints. + ## +in_class_example <- function() { + # Create our initial dataset. + # Note that we use True/False so R knows that each value is either present or not. + items <- c( "A", "B", "C", "D", "E") + values <- c( TRUE, FALSE, TRUE, TRUE, FALSE, + FALSE, TRUE, TRUE, FALSE, TRUE, + TRUE, TRUE, TRUE, FALSE, TRUE, + FALSE, TRUE, FALSE, FALSE, TRUE) + + # Create our dataset in the format the apriori function wants. + dataset <- data.frame(matrix(values, nrow=4, byrow=TRUE)) + colnames(dataset) <- items + + # Generate rules from apriori function. + rules <- apriori(dataset, parameter=list(supp=0.4, conf=0)) + + # Display all rules. + catn('') + catn('') + print(rules) + inspect(rules) +} + + +in_class_example() diff --git a/documents/references.md b/documents/references.md index c6822a2cf63873118a5a4d5752a98ddb0d691b9c..d49cd6b6604835c54cd5bac4e1b80d83ed598625 100644 --- a/documents/references.md +++ b/documents/references.md @@ -11,6 +11,7 @@ All references to external logic. Includes anything from stack overflow links to Turns out that R has "packages", and they already have an implementation of Apriori. Hours wasted trying to implement it myself, rip.<br> * <https://datascienceplus.com/implementing-apriori-algorithm-in-r/> * <https://www.rdocumentation.org/packages/arules/versions/1.6-5/topics/apriori> +* <https://www.rdocumentation.org/packages/arules/versions/1.6-5/topics/rules-class> ### Run Entire Script in R Studio <https://stackoverflow.com/a/12766667>