格式化PDF中的表格,以免列值重复进行汇总

时间:2018-11-16 09:17:31

标签: r

对不起,问题标题令人困惑,我不确定如何标记我要解决的问题。

我正在使用ToothGrowth数据集生成一个表,并使用以下代码将其输出为pdf:

library(grid)
library(extraGrid)

> dput(ToothGrowth)
structure(list(len = c(4.2, 11.5, 7.3, 5.8, 6.4, 10, 11.2, 11.2, 
5.2, 7, 16.5, 16.5, 15.2, 17.3, 22.5, 17.3, 13.6, 14.5, 18.8, 
15.5, 23.6, 18.5, 33.9, 25.5, 26.4, 32.5, 26.7, 21.5, 23.3, 29.5, 
15.2, 21.5, 17.6, 9.7, 14.5, 10, 8.2, 9.4, 16.5, 9.7, 19.7, 23.3, 
23.6, 26.4, 20, 25.2, 25.8, 21.2, 14.5, 27.3, 25.5, 26.4, 22.4, 
24.5, 24.8, 30.9, 26.4, 27.3, 29.4, 23), supp = structure(c(2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("OJ", 
"VC"), class = "factor"), dose = c(0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2)), class = "data.frame", row.names = c(NA, -60L))

pdf("Test1.pdf", height = 20, width = 10)
grid.table(ToothGrowth, rows = NULL, theme = ttheme_minimal())
dev.off()

正如您在输出中看到的(请参见下图),dose和supp列的值在整个过程中都会重复。但是,我想对表格进行格式化,以使剂量和补充值不会在整个过程中重复出现,我还附上了另一张照片,向您展示了我到底想要什么。

在此方面会有所帮助,因为我不确定解决方案是在数据处理还是pdf输出代码中。

现有格式 enter image description here

所需格式

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用:

TG1 <- ToothGrowth  # make a copy in this case, its a "build-in" dataset

inds <- duplicated(TG1[,2:3]) # use duplicated in the respective column combination

TG1[inds, 2:3] <- NA  # replace all dupe rows for your cols 2 and 3

#TG1$supp <- as.character(TG1$supp) # if you want to use "", you need to convert from factor to character first.
#TG1[inds, 2:3] <- "" # try out what works better for you.

pdf("Test1.pdf", height = 20, width = 10)
grid.table(TG1, rows = NULL, theme = ttheme_minimal())
dev.off()