Loop data frame and Merge Two cells ?


Is there a way to loop through a data frame and merge two cells ?
I know how to loop through a data frame, just can't seem to merge the cells because they have the same value. I just want both cell to be one with the one value.

I see many examples of merging two columns.

Please advise !

I do not really understand what you mean by merging individual cells. Do you want to replace the value in one of the cells with NA? Can you explain in detail what the values are before and after the change? And what are you trying to accomplish that requires this "merge"?

Yes after the merge, this is the before on top and after on the bottom result.


That is not possible with a data frame, as far as I know. Data frames are basically lists of columns and every column must have the same number of elements. It is probably possible to display a table with that structure while taking information from the data frame, but I have very little knowledge of that.

As @FJCC said, it's not possible to represent data in this format in a data frame, but if you need to print the table somewhere, there are many options. For instance, huxtable is one such option (https://hughjonesd.github.io/huxtable/design-principles.html has multiple other packages you might want to try out). There is also gt package that definitely allows you to merge cells in any way you want for display.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.