frajuegies frajuegies - 1 year ago 139
R Question

Convert R Dataframe from long to wide format and concatenate string values

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

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download