-1

I have a frame with this format

 "x" "y"
1 A text1
2 A text2
3 A text3
4 B text4
5 B text4
6 B text5
7 C text6

And I need to transform into this:

 "x" "y"    
1 A text1;text2;tex3
2 B text4;text5
3 C text6

Probably can be done with reshape or recast, but im not sure how to keep the texts values in the same row. Thanks!

christian
  • 55
  • 7
  • @thelatemail Good catch, the answer there is a bit more general (dealing with multiple columns). The one answer that uses `paste(., collapse=";")` would likely answer this question. – steveb Jun 13 '16 at 04:15
  • Thanks both. Sometimes is difficult to put an idea from spanish to english and that conspire to do more difficult the search in the rest of the forum. I will try to be more careful next time. – christian Jun 13 '16 at 14:30

2 Answers2

2

You could use dplyr to do this as follows:

library(dplyr)

### Data is set from "dput" output.
data_xy <- structure(list(x = c("A", "A", "A", "B", "B", "B", "C"), y = c("text1", "text2", "text3", "text4", "text4", "text5", "text6")), class = "data.frame", .Names = c("x", "y"), row.names = c(NA, -7L))
data_xy %>%
    group_by(x) %>%
    summarise(y = paste(unique(y), collapse=";"))

##   x                 y
## 1 A text1;text2;text3
## 2 B       text4;text5
## 3 C             text6

## OR

data_xy %>%
    group_by(x) %>%
    summarise_each(funs(paste(unique(.), collapse=";")))

Since your output shows only one occurrence of text4 for B, unique is used.

steveb
  • 5,382
  • 2
  • 27
  • 36
2

We can use data.table

library(data.table)
setDT(df1)[, .(y= toString(y)), by = x]
akrun
  • 874,273
  • 37
  • 540
  • 662