【问题标题】:Two-dimensional table of means in summaryBy in RR中的summaryBy的二维表
【发布时间】:2013-12-01 18:01:21
【问题描述】:

我们来看看这段代码:

library(doBy)
tab <- summaryBy(x ~ A + B, df)

它为 A 和 B 的每个组合计算 x 的平均值。

如何从tab 中创建一个二维表,其中A 在行中,B 在列中,以便行和列的交集为具有给定组合的组提供x 的平均值A & B 的?

【问题讨论】:

  • 您能否提供一些示例数据和您的预期输出?还显示你到目前为止尝试了什么。

标签: r summarization


【解决方案1】:

我会跳过中间步骤并使用旨在提供您想要的功能的功能,即tapply。使用 agsudy 的数据:

> x.mean <- with(dat, tapply(x, list(A=A,B=B), mean))
> x.mean
   B
A           2         3         4         5
  1 0.3671088 0.4531040 0.5942483 0.8013453
  2 0.4776386 0.6115361 0.7907584 0.6607741
  3 0.3966482 0.3447879 0.4372367 0.4914243
  4 0.2779789 0.6780573 0.4087858 0.4205421
  5 0.6288597 0.6924584 0.6508705 0.5648296

如果您真的想使用中间步骤,您也可以使用 tapplyIc 函数进行重新排列:

with(tab, tapply(x.mean, list(A=A,B=B), c))
   B
A           2         3         4         5
  1 0.3671088 0.4531040 0.5942483 0.8013453
  2 0.4776386 0.6115361 0.7907584 0.6607741
  3 0.3966482 0.3447879 0.4372367 0.4914243
  4 0.2779789 0.6780573 0.4087858 0.4205421
  5 0.6288597 0.6924584 0.6508705 0.5648296

> with(tab, tapply(x.mean, list(A,B), I))
          2         3         4         5
1 0.3671088 0.4531040 0.5942483 0.8013453
2 0.4776386 0.6115361 0.7907584 0.6607741
3 0.3966482 0.3447879 0.4372367 0.4914243
4 0.2779789 0.6780573 0.4087858 0.4205421
5 0.6288597 0.6924584 0.6508705 0.5648296

【讨论】:

    【解决方案2】:

    如果我理解我认为您正在寻求重塑您的数据。一种选择是使用来自reshape2 包的dcast

    dat.s <- summaryBy(x~B+A, data=dat)
    library(reshape2)
    dcast(dat.s,A~B)
    

    例如:

    ## create some data 
    set.seed(1)
    dat <- data.frame(x=runif(100),
                      A=sample(1:5,100,rep=TRUE),
                      B=sample(2:5,100,rep=TRUE))
    
    
    library(doBy)
    dat.s <- summaryBy(x~B+A, data=dat)
    dat.s <- round(dat.s,2)   ## for better output
    library(reshape2)
    dcast(dat.s,A~B)
    
      A    2    3    4    5
    1 1 0.37 0.45 0.59 0.80
    2 2 0.48 0.61 0.79 0.66
    3 3 0.40 0.34 0.44 0.49
    4 4 0.28 0.68 0.41 0.42
    5 5 0.63 0.69 0.65 0.56
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多