-2

I have a dataset that looks like the table

attached here

I need to combine the content based on content in the first column. This is how I want my data to be:

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 4
    `aggregate(Message ~ Name, df, paste, collapse = ' ')` or equivalent dplyr or data.table – alistaire Jan 05 '18 at 23:19
  • 2
    Also, please edit [to make your example reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610), i.e. with runnable code to produce data (not pictures) and whatever attempts you've made – alistaire Jan 05 '18 at 23:20
  • Hi Alistaire, Thank you very much for the quick reply. I've been trying to do this with a massive dataset, and I couldn't figure this out. This works fine. I really appreciate the support. – Chamil Rathnayake Jan 05 '18 at 23:26
  • @ChamilRathnayake, I posted my answer below so that other people in the future can use your question to learn from. It's always good to provide [a complete minimal reproducible example](http://stackoverflow.com/help/mcve) to go along with your question, as aalistaire points out above. Something we can work from and use to show you how it might be possible to answer your question. I will also recommend that you to take a look at the [how do I ask a good question](https://stackoverflow.com/help/how-to-ask). It's generally good to demonstrate you already put some effort into it. – Eric Fail Jan 19 '18 at 13:16
  • @EricFail, thanks very much. The link is very useful, and I'm going to use the suggestions in my next question. – Chamil Rathnayake Jan 20 '18 at 23:31
  • if the answer below solved your problems you should mark it as such, i.e. check the green answer mark so that others users can see it. – Eric Fail Jan 30 '18 at 12:44
  • @EricFail, sorry, for some reason I can't figure out how to mark the answer. – Chamil Rathnayake Feb 02 '18 at 11:42
  • If you need help understanding what to do when someone answers your question please read [this](https://stackoverflow.com/help/someone-answers). You should also study [this article, to learn how to produce a complete minimal reproducible example](http://stackoverflow.com/help/mcve) for the next time you need to ask a question. Cheers. – Eric Fail Feb 02 '18 at 11:55

1 Answers1

0
# install.packages(c("tidyverse"), dependencies = TRUE)
library(tibble)
tbl <- tibble(Name = c(rep('Jack', 3L), rep('Ann', 2L)),
              Message = c('Hello', 'How are you?', 'Nice to meet you', 'Hi', "I'm fine")) 
tbl
#> # A tibble: 5 x 2
#>    Name          Message
#>   <chr>            <chr>
#> 1  Jack            Hello
#> 2  Jack     How are you?
#> 3  Jack Nice to meet you
#> 4   Ann               Hi
#> 5   Ann         I'm fine

library(dplyr)
aggregate(Message ~ Name, tbl, paste, collapse = ' ') %>% arrange(desc(Name))
#>   Name                             Message
#> 1 Jack Hello How are you? Nice to meet you
#> 2  Ann                         Hi I'm fine
Eric Fail
  • 8,191
  • 8
  • 72
  • 128