Convert normal R data frame into ragged format (a la ftable)
<p>In <code>R</code> the function <code>ftable()</code> creates by default a table with a so called ragged appearance: </p>

<pre><code>data(UCBAdmissions)
ftable(UCBAdmissions)
</code></pre>

<p>...</p>

<pre><code>                Dept   A   B   C   D   E   F
Admit    Gender                             
Admitted Male        512 353 120 138  53  22
         Female       89  17 202 131  94  24
Rejected Male        313 207 205 279 138 351
         Female       19   8 391 244 299 317
</code></pre>

<p>Rows and columns are “ragged” in the sense that labels are only displayed when they change, with the obvious convention that rows are read from top to bottom and columns are read from left to right. (<a href="https://cran.r-project.org/doc/manuals/r-devel/R-data.html#Flat-contingency-tables" rel="nofollow noreferrer">https://cran.r-project.org/doc/manuals/r-devel/R-data.html#Flat-contingency-tables</a>)</p>

<p><strong>Question:</strong></p>

<p>How can I get same "ragged" appearance for a normal <code>data.frame</code> object? </p>

<p><strong>Reproducible example:</strong></p>

<pre><code>before= data.frame(C1= c(rep("A", 5), rep("L", 2)),
                   C2= c("B", rep("E", 3), rep("K", 2), "L"),
                   C3= c("C", "F", rep("H", 5)),
                   C4= c("D", "G", "I", rep("J", 4)), 
                   stringsAsFactors = FALSE)

before
</code></pre>

<p>...</p>

<pre><code>  C1 C2 C3 C4
1  A  B  C  D
2  A  E  F  G
3  A  E  H  I
4  A  E  H  J
5  A  K  H  J
6  L  K  H  J
7  L  L  H  J
</code></pre>

<p>How does a function look like which converts the object <code>before</code> to a new object <code>after</code> of class <code>data.frame</code>, which is printed to console with <code>print(after)</code> as follows...</p>

<pre><code>  C1 C2 C3 C4
1  A  B  C  D
2     E  F  G
3        H  I
4           J
5     K  H  J
6  L  K  H  J
7     L  H  J
</code></pre>

<p>If necessary, it is acceptable that the left out data are lost for this presentation format.</p>