如何在 R 中的同一列中创建具有均值±SEM 的摘要 table

How do I create a summary table with mean ± SEM in the same column in R

我有这个数据框,其中包含某些物种的各种变量读数。每个物种的每个变量有 6 个读数。我想按种类汇总数据,并让每个单元格显示均值±SEM(或 SD)。这是数据的前 10 行

specie     x.col   otu     h     d     j
   <chr>      <dbl> <dbl> <dbl> <dbl> <dbl>
 1 C. comosa     90    27  2.95 0.2   0.62 
 2 C. comosa     84    25  3.95 0.1   0.62 
 3 C. comosa     96    29  1.95 0.3   0.62 
 4 C. comosa     79    30  3.36 0.152 0.684
 5 C. comosa     82    20  1.36 0.152 0.684
 6 C. comosa     86    40  5.36 0.152 0.684
 7 C. distans    80    41  3.75 0.118 0.699
 8 C. distans    75    32  2.75 0.118 0.699
 9 C. distans    85    50  4.75 0.118 0.699
10 C. distans    65    38  3.77 0.12  0.718  

我尝试使用 dplyr 中的 group_by 来总结 Data <- Data %>% group_by(specie) %>% summarise_all(mean) 但我不确定如何将±SEM(或SD)添加到细胞中。

谢谢。

你可以使用像

这样的东西
library(tidyverse)

df %>% 
  pivot_longer(-c(specie)) %>% 
  group_by(specie, name) %>% 
  summarise(Mean = mean(value),
            SD = sd(value))

数据

df = structure(list(specie = c("C. comosa", "C. comosa", "C. comosa", 
"C. comosa", "C. comosa", "C. comosa", "C. distans", "C. distans", 
"C. distans", "C. distans"), x.col = c(90L, 84L, 96L, 79L, 82L, 
86L, 80L, 75L, 85L, 65L), otu = c(27L, 25L, 29L, 30L, 20L, 40L, 
41L, 32L, 50L, 38L), h = c(2.95, 3.95, 1.95, 3.36, 1.36, 5.36, 
3.75, 2.75, 4.75, 3.77), d = c(0.2, 0.1, 0.3, 0.152, 0.152, 0.152, 
0.118, 0.118, 0.118, 0.12), j = c(0.62, 0.62, 0.62, 0.684, 0.684, 
0.684, 0.699, 0.699, 0.699, 0.71)), row.names = c(NA, -10L), 
class = c("tbl_df", "tbl", "data.frame"))

您可以使用 across :

library(dplyr)

df %>%
  group_by(specie) %>%
  summarise(across(x.col:j, ~mean(.x) + sd(.x)))
  #If you are on older version of dplyr use summarise_at
  #summarise_at(vars(x.col:j), ~mean(.x) + sd(.x))

你可以这样做:

library(dplyr)

Data %>% 
  group_by(specie) %>% 
  summarize(across(everything(), function(x) {
    paste(round(mean(x),2), "\u00b1", round(sd(x), 2))}))

#> # A tibble: 2 x 6
#>   specie     x.col        otu         h           d           j          
#>   <chr>      <chr>        <chr>       <chr>       <chr>       <chr>      
#> 1 C. comosa  86.17 ± 6.08 28.5 ± 6.66 3.16 ± 1.43 0.18 ± 0.07 0.65 ± 0.04
#> 2 C. distans 76.25 ± 8.54 40.25 ± 7.5 3.75 ± 0.82 0.12 ± 0    0.7 ± 0.01

数据

Data <- structure(list(specie = c("C. comosa", "C. comosa", "C. comosa", 
"C. comosa", "C. comosa", "C. comosa", "C. distans", "C. distans", 
"C. distans", "C. distans"), x.col = c(90L, 84L, 96L, 79L, 82L, 
86L, 80L, 75L, 85L, 65L), otu = c(27L, 25L, 29L, 30L, 20L, 40L, 
41L, 32L, 50L, 38L), h = c(2.95, 3.95, 1.95, 3.36, 1.36, 5.36, 
3.75, 2.75, 4.75, 3.77), d = c(0.2, 0.1, 0.3, 0.152, 0.152, 0.152, 
0.118, 0.118, 0.118, 0.12), j = c(0.62, 0.62, 0.62, 0.684, 0.684, 
0.684, 0.699, 0.699, 0.699, 0.71)), row.names = c(NA, -10L), 
class = c("tbl_df", "tbl", "data.frame"))