在两组中对 table 的 header 进行排序

Sorting higher header of a table in two groups

我有以下数据table

 tests0     GROUP    mean0 stderr0 mean7 stederr7 diff.std.mean p.value
   <fct>      <fct>    <dbl>   <dbl> <dbl>    <dbl>         <dbl>   <dbl>
 1 ADAS_CogT0 CONTROL   12.6   0.525  13.6    0.662        -1.15  0.00182
 2 ADAS_CogT0 TRAINING  14.0   0.613  12.6    0.570         1.40  0.00295
 3 PVF_T0     CONTROL   32.1   1.22   31.3    1.45          0.498 0.636  
 4 PVF_T0     TRAINING  31.6   1.37   34.3    1.51         -2.48  0.0102 
 5 ROCF_CT0   CONTROL   29.6   0.893  30.3    0.821        -0.180 0.835  
 6 ROCF_CT0   TRAINING  30.1   0.906  29.5    0.929         0.489 0.615  
 7 ROCF_IT0   CONTROL   12.8   0.563  12.2    0.683         0.580 0.356  
 8 ROCF_IT0   TRAINING  10.9   0.735  12.3    0.768        -1.44  0.0238 
 9 ROCF_RT0   CONTROL   12.1   0.725  12.5    0.797        -0.370 0.598  
10 ROCF_RT0   TRAINING  10.5   0.746  10.9    0.742        -0.534 0.370  
11 SVF_T0     CONTROL   35.5   1.05   34      1.15          1.42  0.107  
12 SVF_T0     TRAINING  34.1   1.04   32.9    1.16          0.962 0.231

因为我想用 flexatable package 创建一个 table 我试图通过创建两个 headers 来对属于 CONTROL 和 TRAINING 的结果进行排序,下面应该列出结果。有人知道我该怎么做吗?

这里是我使用的数据集

> dput(head(out, 50))
structure(list(ID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
45, 46, 47, 48, 49, 50), GROUP = 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, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CONTROL", 
"TRAINING"), class = "factor"), Gender = structure(c(1L, 2L, 
1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L
), .Label = c("M", "F"), class = "factor"), Age = c(74, 76, 81, 
74, 69, 72, 75, 83, 78, 72, 82, 68, 72, 72, 73, 80, 69, 72, 70, 
80, 75, 80, 78, 74, 82, 74, 80, 82, 78, 81, 66, 71, 70, 79, 78, 
73, 72, 77, 77, 71, 83, 74, 70, 71, 77, 69, 67, 64, 79, 71), 
    Education = c(18, 4, 8, 5, 8, 11, 5, 5, 4, 8, 8, 12, 5, 18, 
    13, 5, 13, 13, 5, 5, 13, 5, 3, 8, 17, 5, 8, 5, 5, 8, 17, 
    8, 18, 18, 13, 13, 13, 13, 15, 17, 8, 5, 5, 13, 8, 5, 11, 
    13, 8, 8), tests0 = structure(c(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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("ADAS_CogT0", 
    "PVF_T0", "ROCF_CT0", "ROCF_IT0", "ROCF_RT0", "SVF_T0"), class = "factor"), 
    score0 = c(14.66, 15.33, 17.33, 19, 7.66, 12.6, 18.67, 14.99, 
    17.99, 17.33, 13.66, 16.99, 10.66, 9.66, 14.99, 15.66, 13.33, 
    4.33, 14.33, 15.99, 16.33, 10.66, 14.66, 10.66, 19.33, 17.66, 
    15.99, 20.66, 20.6, 17, 10.33, 6.33, 6.66, 19.99, 13.33, 
    24.33, 12.33, 10.33, 12.33, 9.66, 10.99, 13.99, 23, 6.32, 
    11.32, 13.99, 14.66, 8.99, 14.33, 9.99), tests7 = structure(c(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, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L), .Label = c("ADAS_CogT7", "PVF_T7", "ROCF_CT7", 
    "ROCF_IT7", "ROCF_RT7", "SVF_T7"), class = "factor"), score7 = c(16, 
    9.32, 21.33, 17, 8.32, 11, 14.99, 10.99, 17, 18.33, 13.32, 
    14.34, 8.99, 7, 11.99, 15.33, 6.99, 5.33, 12.32, 13, 21.32, 
    7.99, 13.33, 11.99, 17.32, 16.32, 16.33, 14.66, 18.99, 17.33, 
    7.99, 9.33, 10.99, NA, 12.99, 16.33, 21.66, 9, 9.34, 8.66, 
    8.33, 13.66, 15.66, 6.66, 10.99, 13.33, 13.33, 7.99, 11.99, 
    11.32)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", 
"data.frame"))
> 

提前致谢

这里有一张图你可以画,我希望得到(我想创建第二个听众,作为报告标签'Airquality'和'Time' 和我想正确输入相应结果的地方,两个名称组 CONTROL 和 TRAINING 组).

也许这有帮助

library(dplyr)
library(tidyr)
library(ftExtra)
library(flextable)
df1 %>% 
   pivot_wider(names_from = GROUP, values_from = where(is.numeric), 
       names_glue = "{GROUP}-{.value}")  %>%  
   select(tests0, starts_with("CONTROL"), starts_with("TRAINING")) %>%  
   as_flextable() %>% 
   span_header(sep = "-")  %>%
   autofit(part = "all") %>%
   align(align = "center", part = "all")

-输出

数据

df1 <- structure(list(tests0 = c("ADAS_CogT0", "ADAS_CogT0", "PVF_T0", 
"PVF_T0", "ROCF_CT0", "ROCF_CT0", "ROCF_IT0", "ROCF_IT0", "ROCF_RT0", 
"ROCF_RT0", "SVF_T0", "SVF_T0"), GROUP = c("CONTROL", "TRAINING", 
"CONTROL", "TRAINING", "CONTROL", "TRAINING", "CONTROL", "TRAINING", 
"CONTROL", "TRAINING", "CONTROL", "TRAINING"), mean0 = c(12.6, 
14, 32.1, 31.6, 29.6, 30.1, 12.8, 10.9, 12.1, 10.5, 35.5, 34.1
), stderr0 = c(0.525, 0.613, 1.22, 1.37, 0.893, 0.906, 0.563, 
0.735, 0.725, 0.746, 1.05, 1.04), mean7 = c(13.6, 12.6, 31.3, 
34.3, 30.3, 29.5, 12.2, 12.3, 12.5, 10.9, 34, 32.9), stederr7 = c(0.662, 
0.57, 1.45, 1.51, 0.821, 0.929, 0.683, 0.768, 0.797, 0.742, 1.15, 
1.16), diff.std.mean = c(-1.15, 1.4, 0.498, -2.48, -0.18, 0.489, 
0.58, -1.44, -0.37, -0.534, 1.42, 0.962), p.value = c(0.00182, 
0.00295, 0.636, 0.0102, 0.835, 0.615, 0.356, 0.0238, 0.598, 0.37, 
0.107, 0.231)), class = "data.frame", row.names = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12"))