2

How do I merge cells in the same column in columns 1 to 4 on the condition that cells in columns 1 and 2 are identical? The before is an output after a one-to-many merge. The after is the desired final output.

Before

After

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
Tom
  • 21
  • 1
  • 1
    R does not support "merged" cells like excel, it is best to just keep all the data, as that is what excel does anyway, it just shows you something different – emilliman5 Dec 09 '16 at 20:49
  • Um, no. No it doesn't. If (in a generic sheet) if you merge A1:A3 then refer to =A2 you'll get 0. If you save it as a CSV it'll put blank values for A2 & A3 and it doesn't store a `` node in the sheet XML (if an xlsx file). Please don't spread misinformation. – hrbrmstr Dec 09 '16 at 21:46
  • Having clarified what Excel does and does not do, the only way you could achieve something like this in R is if you use a combination of lists and data.frames which is a nightmare waiting to happen for most folks. Do the only truthful bit in emillman5's comment and keep all the data or keep them separate and merge only when necessary. If you need them merged for *displaying data* then ask the question with that clarification as there are ways to do that with various packages. – hrbrmstr Dec 09 '16 at 21:48
  • It sounds like you want to nest your data, in which case you could try `nest()` from `tidyr`. Take a look at [this question](http://stackoverflow.com/q/15377238/4477364). – Joe Dec 09 '16 at 22:07
  • If you want to do this for output or display purposes, please let us know whether you want to output to html, PDF, Excel spreadsheet, etc. If, on the other hand, you're asking about analyzing the data in R, let us know what you're trying to do and we can help you structure your data appropriately. – eipi10 Dec 10 '16 at 18:07
  • Is your question answered? – and-bri Dec 11 '16 at 16:00

1 Answers1

0

Check out FlexTable in the ReporteRs package. It has a spanFlexTableRowsfunction that does exactly what you're looking for.

Help page here: http://davidgohel.github.io/ReporteRs/articles/FlexTable.html

Admittedly, this is only for presentation of the data, not for working with it.

BLT
  • 2,492
  • 1
  • 24
  • 33