繁体   English   中英

我如何制作一个 latex 表,其中一组因素作为行,另一组因素作为列

[英]How would I make a latex table with one set of factors as the rows and the other set of factors as the columns

我想在 R 中创建一个表,我可以将其转换为 Latex 代码,其中月份作为行的名称,类别作为列的名称。 在对应于月份和类别的每个“单元格”中,我希望将 meantotrob2 和 stdtotrob2 放在 meantotrob2 下面的小括号中。 这是我的 dataframe 的一部分。

label Category meantotrob2 stdtotrob2
  <fct>    <dbl>       <dbl>      <dbl>
1 April        1      0.122       0.361
2 April        2      0.121       0.288
3 April        3      0.123       0.297
4 April        4      0.0996      0.248
5 May          1      0.0878      0.206
6 May          2      0.0776      0.182
Months     1       2          3
# April 0.12162162 0.12111801  0.12278761
        **0.361**   **0.288**  **0.297**
# May   0.08783784 0.07763975  0.09734513
         **0.206** **0.182**   **0.259**

这就是我要找的。 标准错误以粗体显示。 另外,这里是我的dataframe的负责人

structure(list(label = structure(c(1L, 1L, 1L, 1L, 2L, 2L), .Label = c("April", 
"May", "June", "July(1-17)", "July(18-31)", "August", "September", 
"October", "November", "December"), class = "factor"), Category = c(1, 
2, 3, 4, 1, 2), meantotrob2 = c(0.121621621621622, 0.12111801242236, 
0.122787610619469, 0.0995575221238938, 0.0878378378378378, 0.077639751552795
), stdtotrob2 = c(0.361428596863379, 0.287914904504829, 0.297434540363719, 
0.248141256461657, 0.205954891705483, 0.181650473189414)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list(
    label = structure(1:2, .Label = c("April", "May", "June", 
    "July(1-17)", "July(18-31)", "August", "September", "October", 
    "November", "December"), class = "factor"), .rows = structure(list(
        1:4, 5:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))

已编辑:您正在寻找需要多个步骤的答案,您需要首先了解您的 LaTeX 表的外观,以创建一个看起来像将其打印为 LaTeX 表的数据框。

这是一条路径:

  1. 融化你的数据。
melt(df,id = c("Label","Category"),variable.name = "Type",value.name = "value")
  1. 然后取消熔化您的类别数据:检查这篇文章
  2. 按顺序对数据进行order
  3. 使用colnamesLabel重命名为Month
  4. 从具有标准差的行中清除月份名称。
  5. Type=="stdtorob2"时用paste处理标准差字符串
paste0("**",as.character(foo),"**")
  1. 删除Type列,因为您不需要它。 使用tidyverse df %>% select(-Type)应该这样做。
  2. 最后使用xtable将表打印为 LaTeX 代码。 尝试这个:
library(xtable)
print(xtable(df,digits=4), include.rownames=FALSE)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM