我使用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 for data.frames使用mutate的解决方案应该是值得的,我试过这个但是没有用:
mutate(ta, class=ifelse(lag(class,1) == class & !is.na(lag(class,1)), "", class ))
答案 0 :(得分:2)
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