Skip to content

Instantly share code, notes, and snippets.

@revodavid
Created January 30, 2019 12:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revodavid/0975be08eacff40e60a560dd845bc47c to your computer and use it in GitHub Desktop.
Save revodavid/0975be08eacff40e60a560dd845bc47c to your computer and use it in GitHub Desktop.
# Import relevant packages
library(dplyr)
library(tidyverse)
library(arulesSequences)
#Import standard transaction data
transactions = read.csv("transactions.csv")
# Start time of data to be considered
start_month <- "2015-07-01"
# Create list of Azure services by customer ID and CleanMonth (formatted dates)
trans_sequence <- transactions %>%
group_by(CustomerID, CleanMonth) %>%
summarize(
SIZE = n(),
ServiceLevel = paste(as.character(ServiceLevel), collapse = ';')
)
# Make event and sequence IDs into factors
elapsed_months <- function(end_date, start_date) {
ed <- as.POSIXlt(end_date)
sd <- as.POSIXlt(start_date)
12 * (ed$year - sd$year) + (ed$mon - sd$mon)
}
trans_sequence$eventID <- elapsed_months(trans_sequence$CleanMonth, start_month)
trans_sequence = trans_sequence[,c(1,5,3,4)]
names(trans_sequence) = c("sequenceID", "eventID", "SIZE", "items")
trans_sequence <- data.frame(lapply(trans_sequence, as.factor))
trans_sequence <- trans_sequence[order(trans_sequence$sequenceID, trans_sequence$eventID),]
# Convert to transaction matrix data type
write.table(trans_sequence, "mytxtout.txt", sep=";", row.names = FALSE, col.names = FALSE, quote = FALSE)
trans_matrix <- read_baskets("mytxtout.txt", sep = ";", info = c("sequenceID","eventID","SIZE"))
@jenka13all
Copy link

Is the file "transactions.csv" available to users to test the code out themselves?

@0halee
Copy link

0halee commented May 13, 2019

Thanks for your code. It helped a lot

@shivalkarj
Copy link

Thanks,the code was very helpful

@haroonfayyaz17
Copy link

Can you give me the csv file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment