简体   繁体   English

在R中制作漂亮的简单t检验(均值差异)表,可以导出到latex

[英]make nice simple t-test (difference in means) table that can be exported to latex in R

The biggest problem with using methods such as through library(broom)使用through library(broom)等方法最大的问题

library(dplyr)
library(broom)

output <- iris %>% 
  group_by(Species) %>% 
  summarise(tidy(t.test(Sepal.Width)))

Is that it produces a tibble which is essentially a glorified dataframe. I do not want a dataframe, but would prefer a summary-stats table.是它产生了一个 tibble,它本质上是一个美化的 dataframe。我不想要 dataframe,但更喜欢摘要统计表。 One method I found was to convert it to a regular dataframe and then add row names, and then use xtable to export it to latex. There is no good code for this available as far as I am aware and doing this is just inefficient in my opinion.我发现的一种方法是将其转换为常规 dataframe,然后添加行名称,然后使用 xtable 将其导出为 latex。据我所知,没有可用的好代码,这样做在我的工作中效率很低观点。

Another method is to use https://www.datanovia.com/en/blog/how-to-perform-t-test-for-multiple-variables-in-r-pairwise-group-comparisons/ but again, we end up with a tibble which is not ideal for exporting it to latex as I'd basically have to reformat all labels anyway.另一种方法是使用https://www.datanovia.com/en/blog/how-to-perform-t-test-for-multiple-variables-in-r-pairwise-group-comparisons/但同样,我们结束提出一个不适合将其导出到 latex 的 tibble,因为我基本上必须重新格式化所有标签。

Any other suggestions?还有其他建议吗?

The flextable package allows you to easily embed tables into latex files. flextable package 允许您轻松地将表格嵌入到 latex 文件中。 Since you are comparing three groups, you really should be applying corrections like Tukey's HSD since this would be more of an ANOVA comparison anyway.由于您正在比较三个组,因此您真的应该应用像 Tukey 的 HSD 这样的更正,因为无论如何这更像是一个方差分析比较。 I have included the code for that instead, however if you are set on the t-test method you can use the same flextable coding for that purpose.我已经包含了用于此目的的代码,但是如果您设置了 t 检验方法,则可以为此目的使用相同的flextable编码。 First, you can get a very bare bones table like so:首先,您可以像这样得到一个非常简单的表:

#### Tukey HSD Version ####
tukey.output <- iris %>% 
  rstatix::tukey_hsd(Sepal.Width ~ Species)

tukey.output %>% 
  flextable()

However you'll notice it looks fairly sloppy for this specific case because the p values have many decimals and the names are kinda crap:但是您会注意到,对于这种特定情况,它看起来相当草率,因为 p 值有很多小数,而且名称有点乱:

在此处输入图像描述

We can prettify it a bit by adding a fair amount of code.我们可以通过添加大量代码来美化它。 Keep in mind I added some spaces in some of the names because flextable will try to fit the table as best it can into the limited width of the page, so it can sometimes smoosh things together.请记住,我在一些名称中添加了一些空格,因为flextable会尽可能地使表格适应页面的有限宽度,因此它有时可以将东西拼凑在一起。

tukey.output %>% 
  select(-term,
         -null.value) %>% 
  rename(`Species 1` = group1,
         `Species 2` = group2,
         `Estimate` = estimate,
         `CI Lower` = conf.low,
         `CI Upper ` = conf.high,
         ` P Value` = p.adj,
         `P Sig?` = p.adj.signif) %>% 
  mutate(`Species 1` = c("Setosa",
                         "Setosa",
                         "Versicolor"),
         `Species 2` = c("Versicolor",
                         "Virginica",
                         "Virginica"),
         `CI Lower` = round(`CI Lower`,3),
         `CI Upper ` = round(`CI Upper `,3),
         ` P Value` = c(" < 0.00",
                       " < 0.00",
                       " < 0.00")) %>% 
  flextable() %>% 
  add_header_lines(values = "Tukey HSD Table for Iris Dataset")

Which gives you this:这给了你这个:

在此处输入图像描述

I would have put this in a comment but I don't have that ability yet.我会将此放在评论中,但我还没有这种能力。

Have you checked out library(kableExtra) ?你检查过library(kableExtra)吗?

https://bookdown.org/yihui/rmarkdown-cookbook/kableextra.html https://bookdown.org/yihui/rmarkdown-cookbook/kableextra.html

There are style functions that can reformat the presentation of the whole table in one line of code.有一些样式函数可以在一行代码中重新格式化整个表格的表示。

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

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