【问题标题】:Standard Deviation in parenthesis: Latex R括号内的标准差:Latex R
【发布时间】:2020-11-28 18:03:52
【问题描述】:

我想要数据框括号中的标准差。由于我使用的是 Latex,因此我希望输出类似于:meanValue (sdValue)。

我拥有的数据框包含我感兴趣的每个变量的均值和 sd 值列。

例如,如何将标准差放在括号中?

iris %>% group_by(Species) %>% summarize(MeanPetal = mean(Petal.Length), sdPetal = sd(Petal.Length))

【问题讨论】:

  • 已编辑。图片仅供参考。任何具有平均值和标准差的特定表格就足够了。

标签: r latex xtable


【解决方案1】:

在计算sdPetal 时使用一些格式化函数。例如,


library(tidyverse)
iris %>% group_by(Species) %>% summarize(MeanPetal = mean(Petal.Length), 
                                         sdPetal = sprintf("(%.2f)", sd(Petal.Length)))
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 3 x 3
#>   Species    MeanPetal sdPetal
#>   <fct>          <dbl> <chr>  
#> 1 setosa          1.46 (0.17) 
#> 2 versicolor      4.26 (0.47) 
#> 3 virginica       5.55 (0.55)

reprex package (v0.3.0) 于 2020 年 11 月 28 日创建

如果你想在 LaTeX 中使用它,只需将其传递给 knitr::kable


library(tidyverse)
library(knitr)
iris %>% 
    group_by(Species) %>% 
    summarize(MeanPetal = mean(Petal.Length), 
              sdPetal = sprintf("(%.2f)", sd(Petal.Length)),
              .groups = "keep") %>%
    kable(format = "latex") %>% cat
#> 
#> \begin{tabular}{l|r|l}
#> \hline
#> Species & MeanPetal & sdPetal\\
#> \hline
#> setosa & 1.462 & (0.17)\\
#> \hline
#> versicolor & 4.260 & (0.47)\\
#> \hline
#> virginica & 5.552 & (0.55)\\
#> \hline
#> \end{tabular}

reprex package (v0.3.0) 于 2020 年 11 月 28 日创建

(你可能需要也可能不需要cat,这取决于如何 您正在使用此代码。我需要它,因为我正在使用 reprex::reprex 生成 Markdown 代码。)

【讨论】:

  • 这很好!但是我怎么把它带到乳胶中。我不想要乳胶表中的 sdPetal 列(因为 sd 通常显示在括号中)。我查看了 gtsummary,但不幸的是该包没有以乳胶输出。
【解决方案2】:

如果您可以接受 +- 符号以在 LaTeX 中显示标准差,那么有一个包 qwraps2 带有函数 mean_sd(),它计算平均值和标准差并将其转换为乳胶友好的输出格式。输出可以通过包xtable导出到LaTeX。

这是一个示例代码: (只是为了好玩,我添加了Sepal.Length的统计信息)

library(tidyverse)
library(xtable)
library(qwraps2)

df <- iris %>% 
  group_by(Species) %>% 
  summarize(across(c(Petal.Length,Sepal.Length), mean_sd, digits = 3))
df

生成:

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 3
  Species    Petal.Length         Sepal.Length        
  <fct>      <chr>                <chr>               
1 setosa     "1.462 $\\pm$ 0.174" "5.006 $\\pm$ 0.352"
2 versicolor "4.260 $\\pm$ 0.470" "5.936 $\\pm$ 0.516"
3 virginica  "5.552 $\\pm$ 0.552" "6.588 $\\pm$ 0.636"

使用xtable 导出到 LaTeX。

df %>% 
  xtable() %>% 
  print.xtable(type = "latex", 
               sanitize.text.function = function(x){x})

导致LaTeX代码:

% latex table generated in R 3.6.0 by xtable 1.8-4 package
% Mon Jan 11 20:06:07 2021
\begin{table}[ht]
\centering
\begin{tabular}{rlll}
  \hline
 & Species & Petal.Length & Sepal.Length \\ 
  \hline
1 & setosa & 1.462 $\pm$ 0.174 & 5.006 $\pm$ 0.352 \\ 
  2 & versicolor & 4.260 $\pm$ 0.470 & 5.936 $\pm$ 0.516 \\ 
  3 & virginica & 5.552 $\pm$ 0.552 & 6.588 $\pm$ 0.636 \\ 
   \hline
\end{tabular}
\end{table}

编辑---------------

mean_sd()中其实有一种设置括号的方法

df <- iris %>% 
  group_by(Species) %>% 
  summarize(across(c(Petal.Length,Sepal.Length), mean_sd, digits = 3, denote_sd = "paren"))
df

控制台输出:

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 3
  Species    Petal.Length  Sepal.Length 
  <fct>      <chr>         <chr>        
1 setosa     1.462 (0.174) 5.006 (0.352)
2 versicolor 4.260 (0.470) 5.936 (0.516)
3 virginica  5.552 (0.552) 6.588 (0.636)

【讨论】:

  • 谢谢。这正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多