【问题标题】:R melt and aggregateR 熔化和聚集
【发布时间】:2014-07-11 18:00:55
【问题描述】:

我有以下类型的数据:

    sample   X   Y   Z
    A
    B
    A
    C
    ...

存在 A、B 和 C 的多个测量值,并且每个测量值都由一些参数 x、y、z 等表征。

我想得到每个样本的 x,y,z 的平均值和平均值 像这样

    sample   variable   Avg      SE
    A         x       mean(x)   ... 
    A         y       mean(y)   ... 
    A         z       mean(z)   ... 
    ...

【问题讨论】:

  • 这个blog post 有一个估计每组标准误差的例子。另请参阅 CV question 使用线性回归估计标准误差。

标签: r aggregation


【解决方案1】:

使用dplyr怎么样?您可以根据您想要的任何组剖析您的数据,然后返回汇总结果。

mydf <- data.frame(sample = sample(LETTERS[1:4], size = 100, replace = TRUE), 
                   variable = runif(100))

library(dplyr)
mydf %.% group_by(sample) %.% 
  summarize(mean = mean(variable), 
            sd = sd(variable), 
            se = mean(variable) + (sd/sqrt(n())))

Source: local data frame [4 x 4]

  sample      mean        sd        se
1      A 0.4666366 0.2705698 0.5218665
2      B 0.4128302 0.2710152 0.4615059
3      C 0.5055496 0.2569661 0.5616242
4      D 0.5132356 0.2795494 0.5702984

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多