如何在 R 的同一列中创建具有平均± SEM 的汇总表



我有一些物种的各种变量读数的数据框架。每个物种的每个变量有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(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"))

您可以使用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(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"))

最新更新