r - convert normal data set to market basket analysis process-able format -


i had created 1 data set shown below applying market basket analysis(apriori())

id  name 1   mango 1   apple 1   grapes 2   apple 2   carrot 3   mango 3   apple 4   apple 4   carrot 4   grapes 5   strawberry 6   guava 6   strawberry 6   bananas 7   bananas 8   guava 8   strawberry 8   pineapple 9   mango 9   apple 9   blueberries 10  black grapes 11  pomogranate 12  black grapes 12  pomogranate 12  carrot 12  custard apple 

i applied logic convert market basket analysis process-able data.

library(arules) fact <- data.frame(lapply(frt,as.factor)) trans <- as(fact, 'transactions')  

and tried 1 , got error.

trans1 = read.transactions(file = frt, format = "single", sep = ",",cols=c("id","name"))  error in scan(file = file, = "", sep = sep, quiet = true, nlines = 1) :    'file' must character string or connection 

the output got not expected. output got.

items                transactionid 1   {name=mango}                   1   2   {name=apple}                   2   3   {name=grapes}                  3   4   {name=apple}                   4   5   {name=carrot}                  5   6   {name=mango}                   6   7   {name=apple}                   7   8   {name=apple}                   8   9   {name=carrot}                  9   10  {name=grapes}                  10  11  {name=strawberry}              11  12  {name=guava}                   12  13  {name=strawberry}              13  14  {name=bananas}                 14  

my expected output is

id  item 1  {mango,apple,grapes) 2  {apple,carrot} 3  {mango,apple} 

and on this

so can 1 please way expected output(if it's possible)

so helps me apply apriori() algorithm.

thanking in advance.

if doing market basket analysis in arules, need construct transactions. can text file like:

write.csv(frt,file="temp.csv", row.names=false) # "temp.csv" text file tranx <- read.transactions(file="temp.csv",format="single", sep=",", cols=c("id","name")) inspect(tranx) #     items           transactionid # 1  {apple,                       #     grapes,                      #     mango}                    1  # 2  {black-grapes}             10 # 3  {pomogranate}              11 # 4  {black-grapes,                #     carrot,                      #     custard-apple,               #     pomogranate}              12 

...or, if have read text file data.frame, can coerce transactions through list object like:

tranx2 <- list() for(i in unique(frt$id)){   tranx2[[i]] <- unlist(frt$name[frt$id==i]) }  inspect(as(tranx2,'transactions'))  #   items           # 1  {apple,         #   grapes,        #   mango}         # 2  {apple,         #   carrot}        # 3  {apple,         #   mango}         # 4  {apple,         #   carrot,        #   grapes}        

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -