I have a list of transactions in a .csv file.
For each row, the first column contains a "TransactionID"
. In the second columns contains the "ItemName"
of a single item in that transaction. The third column contains the corresponding "ItemNumber"
.
I import those transactions with read.csv
.
trans = read.csv("MyFile.csv", header = TRUE, sep = ";")
Then I take the relevant columns and convert them into a list with factors.
TransList <- split(trans[,"ItemNames"], trans[,"TransactionID"])
Finally I coerce this list to transactions.
TransMat <- as(TransList, "transactions")
Now I want to write the contents of each "basket" into a file.
write(TransMat, file = "Basket.csv", sep = ", ", quote = FALSE)
Unfortunately, instead of writing the original "ItemNames"
into the file, the FactorIDs are written into the file.
What really confuses me, is the fact that this does not happen when I use the "ItemNumbers"
instead of the "ItemNames"
.
How can I write the original "ItemNames" into a .csv file instead of the FactorIDs?
Thanks!
I was able to solve my own question.
Simply coercing to a data.frame before splitting did the trick.
trans <-as.data.frame(trans)
But honestly, I don't understand why this works... If anyone were to enlighten me in a comment I would greatly appreciate it.