Skip to content

Instantly share code, notes, and snippets.

@AdamSpannbauer
Created July 17, 2024 21:05
Show Gist options
  • Save AdamSpannbauer/fb08aaafef3234917f22105bcaf1b777 to your computer and use it in GitHub Desktop.
Save AdamSpannbauer/fb08aaafef3234917f22105bcaf1b777 to your computer and use it in GitHub Desktop.
Helper to convert a transaction object to a df
library(arules)
transaction2df <- function(transation_obj) {
if (!inherits(transation_obj, "transactions")) {
error_msg <- paste0(
"\ntransactions objects only pls\n",
"\ncheck your object with: class(",
substitute(transation_obj),
")"
)
stop(error_msg)
}
trans_list <- as(transation_obj, "list")
basket_dfs <- lapply(seq_along(trans_list), function(i) {
data.frame(
basket_id = i,
item = trans_list[[i]]
)
})
trans_df <- do.call(rbind, basket_dfs)
return(trans_df)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment