frajuegies - 1 year ago 101

R Question

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).

Any help would be really appreciated.

Thank you in advance

Jürgen

Answer Source

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
```