0

I have a data frame such as

df1 <- data.frame(
         A = c("USA","USA","UK","UK","France"),
         B = c("Boston","New York","Cambridge","Oxford","Paris")
)

And I would like to end up with a new data frame:

df2 <- data.frame(
         A = c("USA","UK","France"),
         B = c("Boston; New York","Cambridge; Oxford","Paris")
)

How do I aggregate by A using paste() as the "summarizing" function, or something equivalent to this? Thanks in advance for any help provided.

crlsrns
  • 799
  • 1
  • 5
  • 5
  • 1
    thanks @DavidArenburg. despite my best efforts, i didn't find the question to which you linked and which of course gave me what i needed. – crlsrns Oct 21 '15 at 14:31

1 Answers1

1

I would use data.table for this:

library(data.table)
##
R> (df2 <- data.table(df1)[
  ,.(B = paste0(B, collapse = "; ")),
  by = "A"])
#        A                 B
#1:    USA  Boston; New York
#2:     UK Cambridge; Oxford
#3: France             Paris

And if you insist on having a data.frame for your result, just use setDF(df2) after.

nrussell
  • 18,382
  • 4
  • 47
  • 60