3

I've data in a csv file that looks are follows in R:

    X items
1   1   {Cr
2  NA  D023
3  NA D115}
4   2 {D006
5  NA  D115
6  NA   HTN
7  NA  PGC}
8   3 {D005
9  NA  D010
10 NA  D023
11 NA  D102
12 NA  D214
13 NA   H/C
14 NA   HTF
15 NA    MP
16 NA  NGD}

I want to combine the data from multiple rows into one single cell, values separated by a comma, like:

   X items
   1 {Cr,D023,D115} 
   2 {D006,D115,HTN,PGC}   
   3 {D005,D010,D023,D102,D214,H/C,HTF,MP,NGD}

How to achieve this in R?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
LearneR
  • 2,351
  • 3
  • 26
  • 50
  • 1
    You should load the data into R first. – David Arenburg May 18 '15 at 08:29
  • Yes, of course. But I wanted to show a clear expected output. So, I had to put the Excel picture. – LearneR May 18 '15 at 08:32
  • No you hadn't. See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) how to create a reproducible example using R. Then, take a look [here](http://stackoverflow.com/questions/22685896/using-data-table-plyr-in-r/) for a similar question. – David Arenburg May 18 '15 at 08:32
  • @DavidArenburg I was expecting you to blast out a flawless answer in like 30 seconds, but maybe I was wrong ^ ^. – Tim Biegeleisen May 18 '15 at 08:33
  • 2
    @TimBiegeleisen this is a dupe of many many similar questions, but I'm trying to educate the OP how to use SO properly. – David Arenburg May 18 '15 at 08:34
  • @DavidArenburg Thanks for the suggestion. Tried my best to give a proper example. – LearneR May 18 '15 at 08:42
  • Do you really have `{` and `}` in your data? – David Arenburg May 18 '15 at 08:43
  • @DavidArenburg Yes.. They are present in the data. But they're not important in the output.. (Sorry, let me also remove them from the example output.) – LearneR May 18 '15 at 08:44
  • Try `aggregate(items ~ cumsum(!is.na(X)), df, toString)` (if your data called `df`). You can leave the curly brackets, its fine, I was just trying to understand if they supposed to be there – David Arenburg May 18 '15 at 08:46
  • @DavidArenburg It worked almost perfectly, but from 100th record onwards, a single digit ( from 0-9 cycle) is being added infront of each row's data. Like this: `100 0 {DVME005, DVME023, DVME115, NGR Detector}` `101 1 {Collar, DVME123, He Tube Far, PNG-C, Stabilizer}` `102 2 {DVME005, DVME022, DVME023, DVME215, DVME316, PNG-C, SSn721}` Any idea why? – LearneR May 18 '15 at 08:53
  • OK, I've added some alternatives in my answer. Check it out. Either way, I don't have your real data, so I'm not sure what cause the error. – David Arenburg May 18 '15 at 08:55
  • 1
    @DavidArenburg Thank you so much for the answer and the alternatives.. – LearneR May 18 '15 at 08:56
  • Hmm, haven't you asked this already ?http://stackoverflow.com/questions/30235606/how-to-combine-rows-based-on-unique-values-in-r – David Arenburg May 18 '15 at 09:26

0 Answers0