生成没有重复值的 pandoc table

Generate a pandoc table without repeated values

我使用pander来生成pandoc风格的表,我经常需要抑制变量的重复值,就像这个例子:

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)

输出为

    --------------------
    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  
  --------------------

我想

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

                  11  

                   8  

    C2      V      7  

                   7  

                   5  

            C     12  

                   9  

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

问题与此类似Removing Locally Repeated Values,当我将 dplyr 用于 data.frames 时,使用 mutate 的解决方案应该是值得的,我尝试了这个但没有成功:

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

This seems like an open bug that will be corrected.与此同时,此解决方法似乎可以完成工作:

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