Search code examples
rmarket-basket-analysis

Rearranging data in R for market basket analysis


I have data available in csv format.

Data format is as follows. With Receipt nos in one column and Product in the corresponding column

Receipt_no Product
A1  Apple
A1  Banana
A1  Orange
A2  Pineapple
A2  Jackfruit
A3  Cola
A3  Tea

I want to rearrange them as

A1 ,  Apple, Banana, Orange
A2 , Pineapple, Jackfruit
A3 , Cola, Tea

That is the receipt numbers and product names in one row separated by comma. Since the data is large I want to rearrange the same in R .

Kindly help

Thanks.

Regards, Nithish


Solution

  • Base R,

    aggregate(Product ~ Receipt_no, df, paste, collapse = ',')
    

    Using dplyr,

    df %>% 
      group_by(Receipt_no) %>% 
      summarise(new = paste(Product, collapse = ','))