0

I have a data frame like this:

df <- data.frame (Subject = c(1,1,1,2,2,3,3), Product = c("A","B","C","B","C","A","D")

and I would like to replace it (into a "wide" format) like this:

df_replace <- data.frame(Subject = c(1,2,3), Product = c("A;B;C","B;C","A;D"))

The new Product value should be the concatenated string of the original Product values for the given Subject.

I am experimenting with dcast ... but I have not found a way to go forward. (but I am assuming... it is simple).

Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
  • 1
    like this `df %>% group_by(subject) %>% summarise(products=paste(product, collapse=";"))` add a `unique` in the paste if you can have several identical products per subject – agenis Nov 07 '16 at 13:59

1 Answers1

1

You can use aggregate.

xy <- read.table(text = "Subject Product
1   ProdA
1   ProdB
1   ProdC
2   ProdB
2   ProdC
2   ProdD
3   ProdA
3   ProdB", header = TRUE)

aggregate(Product ~ Subject, FUN = paste, collapse = ";", data = xy)

  Subject           Product
1       1 ProdA;ProdB;ProdC
2       2 ProdB;ProdC;ProdD
3       3       ProdA;ProdB
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197