2017-02-19 33 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])}}