【问题标题】:I would like to calculate z-scores based on two factors, is there a way to do this?我想根据两个因素计算 z 分数,有没有办法做到这一点?
【发布时间】:2013-12-30 05:41:58
【问题描述】:

我想根据两个因素计算总和量表的 z 分数:性别和年龄组(四个因素)。

如何在 R 中做到这一点?我对R真的很陌生,才开始学习,我遇到了

tapply(AgingData$StandMen, list(AgingData$AgeGroups, AgingData$Gender_2),
      FUN= "scale")

但结果不是数据框。我怎样才能把它变成一个数据框?或者有没有其他办法?

【问题讨论】:

  • 欢迎来到 SO。请阅读this

标签: r tapply


【解决方案1】:

如果您提供可重现的示例并显示预期的输出,答案可能会更好。也就是说tapply的结果

res <- tapply(AgingData$StandMen, list(AgingData$AgeGroups, AgingData$Gender_2),
      FUN= "scale")

是一个有维度的数组:

nelevls(AgingData$AgeGroups) x nlevles(AgingData$Gender_2)

在这里您可以使用

将其转换为 data.frame
as.data.frame(res)

但我怀疑你正在寻找什么,我想你也想得到长格式的结果:

你使用 reshape2 包来转换结果:

library(reshape2)
melt(res)

或者你使用,你不使用tapply,你从plyr尝试ddply

library(plyr)
ddply(AgingData,.(AgeGroups,Gender_2),transform,scale)

【讨论】:

    猜你喜欢
    • 2021-07-09
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    相关资源
    最近更新 更多