-1

I want standard deviation in the parenthesis for a data frame. Since I'm using Latex, I want the output in something like: meanValue (sdValue).

The data frame that I have contains columns for mean and sd values for each variable that I'm interested in.

For example, how would you put standard deviation in parenthesis?

iris %>% group_by(Species) %>% summarize(MeanPetal = mean(Petal.Length), sdPetal = sd(Petal.Length))
Pss
  • 553
  • 4
  • 12
  • Edited. The image was shown only for reference. Any particular table with that has mean and standard deviation would suffice. – Pss Nov 28 '20 at 18:52

2 Answers2

0

Use some formatting function when you calculate sdPetal. For example,


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)

Created on 2020-11-28 by the reprex package (v0.3.0)

If you want this in LaTeX, just pass it through 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}

Created on 2020-11-28 by the reprex package (v0.3.0)

(You may or may not need the cat at the end, depending on how you are using this code. I needed it because I was producing Markdown code using reprex::reprex.)

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • This is good! But how do I take this to latex. I don't want the column sdPetal in latex table (since sd's are usually shown in the parenthesis). I looked at gtsummary but unfortunately the package does not output in latex. – Pss Nov 28 '20 at 20:08
0

If you can accept an +- symbol to display standard deviation in LaTeX, then there is a package qwraps2 with the function mean_sd(), which calculates and converts the mean and standard deviation to a latex friendly output format. The output can be exported by the package xtable to LaTeX.

Here is an example code: (Just for fun, I've added the statistics for 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

Which generates:

`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"

Exporting to LaTeX using xtable.

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

Leading to LaTeX code:

% 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}

EDIT ---------------

There is actually a way to set parenthesis in mean_sd()

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

Console output:

`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)
Liv-Con
  • 21
  • 5