在R
环境中,我从一组模型中得到tibble()
个参数估算值:
Model term estimate
<chr> <chr> <chr>
1 Equation all_rev ECT -0.175153366498138
2 Equation all_rev Intercept -0.813722280439735
3 Equation all_rev all_rev -1 -0.552033103080694
4 Equation all_rev mood -1 0.149186002525841
5 Equation all_rev zsc_med -1 9.67754263298424
6 Equation all_rev zmq_med -1 4.8674015065453
7 Equation all_rev all_rev -2 -0.286127265624132
8 Equation all_rev mood -2 0.278054170570161
9 Equation all_rev zsc_med -2 -2.36086942618573
10 Equation all_rev zmq_med -2 -2.22689475852024
... ... ... ...
将表转换为类似于列联表的格式的有效方法是什么?如:
Model term estimate
-------------------------------------------------
1 Equation all_rev ECT -0.175153366498138
Intercept -0.813722280439735
all_rev -1 -0.552033103080694
mood -1 0.149186002525841
zsc_med -1 9.67754263298424
zmq_med -1 4.8674015065453
all_rev -2 -0.286127265624132
mood -2 0.278054170570161
zsc_med -2 -2.36086942618573
zmq_med -2 -2.22689475852024
2 Equation mood ECT -0.175153366498138
Intercept -0.813722280439735
all_rev -1 -0.552033103080694
mood -1 0.149186002525841
zsc_med -1 9.67754263298424
zmq_med -1 4.8674015065453
all_rev -2 -0.286127265624132
mood -2 0.278054170570161
zsc_med -2 -2.36086942618573
zmq_med -2 -2.22689475852024
... ... ...
基本上,如果行中有重复,我想只显示第一行值。由于这不是一个列联表,主要的单元格是估计值,而不是频率,因此无法使用table()
命令或ftable()
。
特别是,我想使用knitr::kable()
将结果编织到一个可以在PDF文档中显示的表中。
答案 0 :(得分:2)
您可以使用空字符串替换重复值。例如:
# Fake data
set.seed(2)
dat = data.frame(Model=rep(c("Equation all_rev", "Equation mood"), each=6),
Term=rep(LETTERS[1:6], 2),
Estimate=rnorm(12),
stringsAsFactors=FALSE)
library(knitr)
library(dplyr)
kable(dat %>% mutate(Model = ifelse(duplicated(Model), "", as.character(Model))))
|Model |Term | Estimate| |:----------------|:----|----------:| |Equation all_rev |A | -0.8969145| | |B | 0.1848492| | |C | 1.5878453| | |D | -1.1303757| | |E | -0.0802518| | |F | 0.1324203| |Equation mood |A | 0.7079547| | |B | -0.2396980| | |C | 1.9844739| | |D | -0.1387870| | |E | 0.4176508| | |F | 0.9817528|
有时,您可能需要在子组中删除重复值的多个分层列。 duplicated
单独在这种情况下不起作用。有几种方法可以继续。这是一个例子:
# Add a second column with repeating sub-sets of the Model column
dat = dat %>% mutate(Sub_Model=rep(rep(c("Mal","Serenity"), each=3),2)) %>%
select(1,4,2,3)
# Remove repeated values by group
dat[1:nrow(dat) %% 6 != 1, 1] = ""
dat[1:nrow(dat) %% 3 != 1,2] = ""
kable(dat)
|Model |Sub_Model | Estimate|Term | |:----------------|:---------|----------:|:----| |Equation all_rev |Mal | -0.8969145|A | | | | 0.1848492|B | | | | 1.5878453|C | | |Serenity | -1.1303757|D | | | | -0.0802518|E | | | | 0.1324203|F | |Equation mood |Mal | 0.7079547|A | | | | -0.2396980|B | | | | 1.9844739|C | | |Serenity | -0.1387870|D | | | | 0.4176508|E | | | | 0.9817528|F |
如果您使用xtable
代替kable
,还可以添加其他格式,例如水平线或部分水平线。