如何在R中将订单数据合并为一行

如何在R中将订单数据合并为一行,r,dataframe,plyr,R,Dataframe,Plyr,我有一个如下所示的数据集。数据集取自Kaggle: 我希望将同一订单的行合并为一行,以便将已拆分为不同行的所有订单的订单ID、客户名称、段、类别和子类别合并为一行,并在该行的单个字段中连接订购的项目 我试过这个: df_supstore_order_list <- ddply(supstore_dfcopy,c("Customer.Name", "Product.Name", "Customer.ID", "Segment", "Category", "Sub.Category"), f

我有一个如下所示的数据集。数据集取自Kaggle:

我希望将同一订单的行合并为一行,以便将已拆分为不同行的所有订单的订单ID、客户名称、段、类别和子类别合并为一行,并在该行的单个字段中连接订购的项目

我试过这个:

df_supstore_order_list <- ddply(supstore_dfcopy,c("Customer.Name", "Product.Name", "Customer.ID", "Segment", "Category", "Sub.Category"), function(supstore_dfcopy)paste(supstore_dfcopy$Product.Name,supstore_dfcopy$Customer.ID, supstore_dfcopy$Segment, supstore_dfcopy$Category, supstore_dfcopy$Sub.Category                                               collapse = ","))

正如您所看到的,客户名称等并没有像我所期望的那样合并到一个列中。关于如何做到这一点,您有什么建议吗?

虽然您想要结合什么还不太清楚,但据我所知,您想要结合什么

  • 将客户标识符分为1列
  • 将产品标识符分为1列
  • 例如,对于同一个人下多个订单,您希望将这些订单合并到一行

    因此,

    Name   ID   Item       Category    ItemsOrdered
    John    1   book ----> John, 1     book, toy
    John    1   toy
    
    所以假设这个假设是正确的(如果不是,请告诉我)

    df%
    #按列分组,这些列标识您希望在同一行中显示的项目
    分组人(姓名、id)%>%
    #用“,”将所有项目粘贴在一起
    摘要(项目排序=粘贴(项目,折叠=','))%>%
    #合并按其分组的列
    unite(列=类别、名称、id、sep='、')
    #一个tibble:2x2
    分类项目排序
    1简,2帽子,衬衫
    两个约翰,一把椅子,一张桌子
    
    能否提供一个可复制的数据集示例(请参见此链接:)@dc37谢谢。我对这一切都不熟悉。我现在已经直接加入了数据链接。那会有帮助吗?这里是链接:你需要注册才能下载这些数据,这样对其他人就没有什么帮助了。请提供
    dput(head(supstore_df,10))
    的输出,这将更有帮助。我尝试过了,结果超出了正文的字数。删除您提供的第一个exmaple数据,它将减少您问题中文本的大小
    head(df_supstore_order_list, 5)
        Customer.Name                                                Product.Name Customer.ID
    1     Claire Gute                           Bush Somerset Collection Bookcase    CG-12520
    2     Claire Gute Hon Deluxe Fabric Upholstered Stacking Chairs, Rounded Back    CG-12520
    3 Darrin Van Huff   Self-Adhesive Address Labels for Typewriters by Universal    DV-13045
    4  Sean O'Donnell               Bretford CR4500 Series Slim Rectangular Table    SO-20335
    5  Sean O'Donnell                              Eldon Fold 'N Roll Cart System    SO-20335
        Segment        Category Sub.Category
    1  Consumer       Furniture    Bookcases
    2  Consumer       Furniture       Chairs
    3 Corporate Office Supplies       Labels
    4  Consumer       Furniture       Tables
    5  Consumer Office Supplies      Storage
                                                                                                       V1
    1                             Bush Somerset Collection Bookcase CG-12520 Consumer Furniture Bookcases
    2      Hon Deluxe Fabric Upholstered Stacking Chairs, Rounded Back CG-12520 Consumer Furniture Chairs
    3 Self-Adhesive Address Labels for Typewriters by Universal DV-13045 Corporate Office Supplies Labels
    4                    Bretford CR4500 Series Slim Rectangular Table SO-20335 Consumer Furniture Tables
    5                            Eldon Fold 'N Roll Cart System SO-20335 Consumer Office Supplies Storage
    
    Name   ID   Item       Category    ItemsOrdered
    John    1   book ----> John, 1     book, toy
    John    1   toy
    
    df <- data.frame(name = c('John', 'John', 'Jane', 'Jane'), id = c(1, 1, 2, 2), item = c('chair' , 'desk', 'hat' , 'shirt'))
    
    df %>% 
      # Group by columns that identify Items you would like in the same row
      group_by(name, id) %>% 
      # paste together all items with ", "
      summarise(ItemsOrdered = paste(item, collapse = ', ')) %>% 
      # Unite the Columns you grouped by
      unite(col = Category, name, id, sep = ', ') 
    
    # A tibble: 2 x 2
      Category ItemsOrdered
      <chr>    <chr>       
    1 Jane, 2  hat, shirt  
    2 John, 1  chair, desk