在 Kable 中使用 "pack_rows" 对每 N 行进行分组/打包

Group / pack every N rows using "pack_rows" in Kable

我正在尝试将矢量与 kable 中的“pack_rows”(或“group_rows” - 无论哪个有效)结合使用,以始终如一地 'group' 预定义行数。

我有一个名为“stimuli_list”的字符向量,按字母顺序排列,包含许多单独项目的名称(例如“anchor”、“apple”、“ashtray”)- 太多无法定义一个一。对于这些单独的项目中的每一个,我在一个单独的 df 中有 6 行相应的数据(也按字母顺序排列)。我想将所有 6 行数据分组或打包在它们对应的单个项目标签下。我想要的输出看起来像:

            col1    col2      col3
anchor
            row 1   of data   for anchor
            row 2   of data   for anchor
            row 3   of data   for anchor
            row 4   of data   for anchor
            row 5   of data   for anchor
            row 6   of data   for anchor
apple
            row 1   of data   for apple
            row 2   of data   for apple
            row 3   of data   for apple
            row 4   of data   for apple
            row 5   of data   for apple
            row 6   of data   for apple
and so on...

但是因为我不知道如何定义要分组的行数,所以我得到了这个:

            col1    col2      col3
anchor
            row 1   of data   for anchor
apple
            row 2   of data   for anchor
ashtray
            row 3   of data   for anchor
balloon
            row 4   of data   for anchor
banana
            row 5   of data   for anchor
barrel
            row 6   of data   for anchor
basket
            row 1   of data   for apple
bear
            row 2   of data   for apple
and so on...

由于所有数据都已正确排序,我只需要告诉 pack_rows 在每 6 行而不是每一行上完成它的工作。我目前拥有的代码:

    b %>%
      kable("latex", longtable = T, booktabs = T, linesep = c("", "", "", "", "", "\addlinespace")) %>%
      kable_styling(bootstrap_options = c("striped","condensed")) %>%
      kable_styling(latex_options = c("repeat_header"), font_size = 6) %>%
      add_header_above(c(" " = 1, "Familiarity" = 2, "Visual Complexity" = 2, 
                         "Colour Diagnosticity" = 2, "Mental Imagery Agreement" = 2)) %>%
      pack_rows(index = table(a))

感谢任何帮助!


代表:

stimuli_list:

a <- structure(list(stim = c("anchor", "apple", "ashtray", "balloon", 
"banana", "barrel", "basket", "bear", "bell", "belt")), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

主要数据:

b <- structure(list(Photograph = c("anchor1-photo-colour", "anchor1-photo-grey", 
"anchor2-photo-colour", "anchor2-photo-grey", "anchor3-photo-colour", 
"anchor3-photo-grey", "apple1-photo-colour", "apple1-photo-grey", 
"apple2-photo-colour", "apple2-photo-grey"), Fam_Mean = c(3.55, 
2.97, 3.3, 2.81, 3.52, 3.1, 4.79, 4.85, 4.95, 4.4), Fam_SD = c(1.32, 
1.52, 1.56, 1.57, 1.33, 1.52, 0.49, 0.49, 0.22, 0.88), VisCom_Mean = c(2.95, 
2.81, 3.45, 3, 2.4, 2.35, 3.1, 2.19, 3, 2.4), VisCom_SD = c(1.12, 
0.93, 1.23, 0.97, 0.88, 1.09, 1.14, 1.08, 0.97, 0.99), ColDia_Mean = c(2.81, 
NaN, 3.5, NaN, 2.7, NaN, 3.43, NaN, 3.5, NaN), ColDia_SD = c(1.21, 
NA, 1.19, NA, 1.3, NA, 1.08, NA, 1.32, NA), MenIma_Mean = c(4.05, 
3.95, 3.75, 3.52, 3.8, 4.25, 4.15, 3.05, 3.76, 2.75), MenIma_SD = c(1.05, 
0.89, 1.25, 1.33, 1.01, 0.79, 0.88, 1.23, 1.04, 1.45)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

您的 a 代表 10 个不同的 stim,但您的 b 似乎只查看其中的两个(anchorapple)。此外,table(a) 只会进入 return 很多 1,所以 kable 一次只会对一行进行分组。

我们需要的机制能够以某种方式识别 anchor1-photo-colour 中的 anchor 以提取它。如果在你们这一代 b 的早期有一种不用正则表达式就可以做到这一点的方法,那么在那里做可能会更好,因为正则表达式如果做得不正确会引入问题。

如果我们假设所有 anchor[0-9].* 张照片是并发的(其他 stim 也是如此),那么我们可以这样做:

table(gsub("[0-9].*", "", b$Photograph))
# anchor  apple 
#      6      4 

这是我们可以传递给 pack_rows 的东西。

为了简单起见,我将演示如何使用 html 引擎,它应该与您的 "latex" 输出相同。

b %>%
  kable("html", longtable = T, booktabs = T, linesep = c("", "", "", "", "", "\addlinespace")) %>%
  kable_styling(bootstrap_options = c("striped","condensed")) %>%
  kable_styling(latex_options = c("repeat_header"), font_size = 6) %>%
  add_header_above(c(" " = 1, "Familiarity" = 2, "Visual Complexity" = 2, 
                     "Colour Diagnosticity" = 2, "Mental Imagery Agreement" = 2)) %>%
  pack_rows(index = table(gsub("[0-9].*", "", b$Photograph)))