Generate a pandoc table without repeated values

70 views Asked by At

I use pander to generate pandoc style tables, often I need to suppress repeated values of variables like this example:

ta <- data.frame(class=c(rep("C1" ,3), rep("C2", 6)),
                 name=rep(c("L", "V", "C"), each=3), num=rpois(9, 10))

pandoc.table(ta)

The output is

    --------------------
    class   name   num 
    ------- ------ -----
    C1      L     15  

    C1      L     11  

    C1      L      8  

    C2      V      7  

    C2      V      7  

    C2      V      5  

    C2      C     12  

    C2      C      9  

    C2      C     12  
  --------------------

and I would like

    --------------------
    class   name   num 
    ------- ------ -----
    C1      L     15  

                  11  

                   8  

    C2      V      7  

                   7  

                   5  

            C     12  

                   9  

                  12  
  --------------------

The problem is similar to this Removing Locally Repeated Values and as I use dplyr for data.frames a solution using mutate should be worth, I tried this but did not work:

mutate(ta, class=ifelse(lag(class,1) == class & !is.na(lag(class,1)), "", class ))
1

There are 1 answers

0
Carlos Cinelli On BEST ANSWER

This seems like an open bug that will be corrected. In the meantime this workaround seems to do the job:

mutate(ta, class=ifelse(levels(class)[lag(class,1)] == class & !is.na(levels(class)[lag(class,1)]), "", class))
  class name num
1     1    L   8
2          L  14
3          L  12
4     2    V  11
5          V   7
6          V  15
7          C  16
8          C   8
9          C   9