R 中数据集的分组依据和均值

Group by and mean on dataset in R

我想计算不同三次重复的平均值,但似乎找不到好的方法。我已经尝试对我的数据集进行分组和总结,但仍然没有成功。我已将以下代码用于 trim 我的数据集:

Data_mini <- 
  Trimmed_Data %>% 
  group_by(Component.Name, Sample.Name) %>%
  summarise(Area.Ratio)
        summarise(DF_ABCR, mymean = mean(Area.Ratio))

我有一个虚拟数据集,其中包含成分名称、样本名称和面积比。我需要计算每个组件名称的 A1_ABCR1、B1_ABCR1 和 C1_ABCR1(样本名称)的平均值。有关数据集的直观概述,请参见图像。谁能分享他们对如何最好地解决这个问题的看法?提前谢谢你:) Dummy dataset visual overview

我创建了一个示例数据:

Trimmed_Data <- data.frame(Sample.Name = c("A1_ABCR1", "B1_ABCR1", "C1_ABCR1", "A1_ABCR1", "B1_ABCR1", "C1_ABCR1"),
                             Component.Name = c("BAD_acetylcarnitine", "BAD_acetylcarnitine", "BAD_acetylcarnitine", "BAD_alanine", "BAD_alanine", "BAD_alanine"),
                             Area.Ratio = runif(6, 0, 100))

示例数据:

  Sample.Name      Component.Name Area.Ratio
1    A1_ABCR1 BAD_acetylcarnitine   70.51099
2    B1_ABCR1 BAD_acetylcarnitine   48.85098
3    C1_ABCR1 BAD_acetylcarnitine   36.42945
4    A1_ABCR1         BAD_alanine   22.29663
5    B1_ABCR1         BAD_alanine   63.06249
6    C1_ABCR1         BAD_alanine   78.02675

您可以使用此代码计算平均单位面积比:

Data_mini <- 
    Trimmed_Data %>% 
    group_by(Component.Name, Sample.Name) %>%
    summarise(mean.Area.Ratio = mean(Area.Ratio))
  
  Data_mini

输出:

# A tibble: 6 × 3
# Groups:   Component.Name [2]
  Component.Name      Sample.Name mean.Area.Ratio
  <chr>               <chr>                 <dbl>
1 BAD_acetylcarnitine A1_ABCR1               70.5
2 BAD_acetylcarnitine B1_ABCR1               48.9
3 BAD_acetylcarnitine C1_ABCR1               36.4
4 BAD_alanine         A1_ABCR1               22.3
5 BAD_alanine         B1_ABCR1               63.1
6 BAD_alanine         C1_ABCR1               78.0