2017-02-19 77 views
0

我是R的初学者,我非常感谢所有帮助。我有一些数据,我清理了一些数据,删除了重复数据等。现在,我有一个CSV文件中的这些数据,我的主要问题是我无法将其转换为事务。我只能将它们转换成列表,但不能作为交易。将csv数据转换为arules和购物篮分析的交易

Please let me know how I can change this data to

This data (like as transactions)

请帮帮忙,TIA!

+1

http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Rentrop

+0

看阅读的手册页。使用'? read.transactions'。有一个例子完全符合你的想法。 –

回答

0

要将csv文件转换为事务文件,请使用此代码。 假设tfile是CSV文件的名称

colnames = names(tfile) 

bas_str = "" 

for(row in 1:nrow(x)){ 


    if(row !=1){       
      bas_str = paste0(bas_str, "\n")} 
    bas_str = paste0(bas_str,row,",")  
    for(col in 2:length(colnames)){  
      if(col != 2){      
        bas_str = paste0(bas_str,",")} 
    bas_str = paste0(bas_str,colnames[col],"=",tfile[row,col])}}