Home > Mobile >  How to group rows based on ID in R
How to group rows based on ID in R

Time:12-07

I want to group my rows with the same ID. The ID's are the transaction number, where every transaction can have several products, with every product per transaction on a different row. I want all products in a transaction (thus all rows with the same ID) in columns, so that I will get columns "ID", "product 1", "product 2", etc.

Thanks in advance!

CodePudding user response:

library(tidyverse)

df <- data.frame(
  transaction = c("a","a", "b", "b","b"),
  product = c("c", "d","e","f","g")
)

df %>% 
  group_by(transaction) %>% 
  summarise(prduct = paste(product, collapse=","))
#> # A tibble: 2 x 2
#>   transaction prduct
#>   <chr>       <chr> 
#> 1 a           c,d   
#> 2 b           e,f,g

Created on 2021-12-06 by the reprex package (v2.0.0)

CodePudding user response:

Using aggregate you could try this:

aggregate(Product ~ Id, df, function(x) paste(unique(x), collapse = '; '))

Data:

df<-data.table("Id"=c(123, 345, 456, 345, 123, 123),
              "Product"=c("Product 1", "Product 2", "Product 1", "Product 1", "Product 1", "Product 3"))

Output:

  Id              Product
1 123 Product 1; Product 3
2 345 Product 2; Product 1
3 456            Product 1
  •  Tags:  
  • r
  • Related