【问题标题】:How do I create a summary table with mean ± SEM in the same column in R如何在 R 的同一列中创建具有平均值 ± SEM 的汇总表
【发布时间】:2020-12-07 08:45:18
【问题描述】:

我有这个数据框,其中包含某些物种的各种变量的读数。每个物种的每个变量有 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  

我尝试使用来自dplyrgroup_by 进行总结 Data &lt;- Data %&gt;% group_by(specie) %&gt;% summarise_all(mean) 但我不知道如何在单元格中添加 ± SEM(或 SD)。

谢谢。

【问题讨论】:

    标签: r dataframe dplyr summary


    【解决方案1】:

    你可以这样做:

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

    【讨论】:

      【解决方案2】:

      你可以使用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))
      

      【讨论】:

        【解决方案3】:

        你可以使用类似的东西

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-22
          • 2017-08-16
          • 2021-10-15
          相关资源
          最近更新 更多