## create a demo file using basket format for the example
data <- paste(
"# this is some test data",
"item1, item2",
"item1",
"item2, item3",
sep = "\n"
)
cat(data)
write(data, file = "demo_basket.txt")
## read demo data (skip the comment in the first line)
tr <- read.transactions("demo_basket.txt", format = "basket", sep = ",", skip = 1)
inspect(tr)
## make always sure that the items were properly separated
itemLabels(tr)
## create a demo file using single format for the example
## column 1 contains the transaction ID and column 2 contains one item
data <- paste(
"trans1 item1",
"trans2 item1",
"trans2 item2",
sep = "\n"
)
cat(data)
write(data, file = "demo_single.txt")
## read demo data
tr <- read.transactions("demo_single.txt", format = "single", cols = c(1, 2))
inspect(tr)
## create a demo file using single format with column headers
data <- paste(
"item_id;trans_id",
"item1;trans1",
"item1;trans2",
"item2;trans2",
sep = "\n"
)
cat(data)
write(data, file = "demo_single.txt")
## read demo data
tr <- read.transactions("demo_single.txt",
format = "single",
header = TRUE, sep = ";", cols = c("trans_id", "item_id")
)
inspect(tr)
## tidy up
unlink("demo_basket.txt")
unlink("demo_single.txt")
Run the code above in your browser using DataLab