【问题标题】:R question - How to extract attributes values from bystat object and place them in variablesR问题-如何从bystat对象中提取属性值并将它们放入变量中
【发布时间】:2010-10-25 18:40:36
【问题描述】:

我正在使用 R 中 Hmisc 包中的 bystat 函数。如何提取属性值并将它们放入变量中。例如,我想计算变量 aaf 的均值和 SD,并将它们放入数据框或矩阵中。

t <- with(d.aaf,bystats(y=aaf,plot_bid,fun=function(x) { 
       c(Mean = round(mean(x),digits=2),SD = round(sd(x),digits=2)) 
     })) 

> str(t)
 bystats [1:121, 1:3] 5 5 5 5 5 4 5 5 3 4 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:121] "P00000000006001288020278" "P00000000006001288085814"
                    "P00000000006001288151350" "P00000000006001288216886" ...
  ..$ : chr [1:3] "N" "Mean" "SD"
 - attr(*, "heading")= chr "function(x) { c(Mean = round(mean(x),digits=2),
                                            SD = round(sd(x),digits=2)) } 
                            of aaf by plot_bid"
 - attr(*, "byvarnames")= chr "plot_bid"

我这样做的方式是首先将“t”转换为数据框,我认为这不是很有效。 感谢您的建议。

【问题讨论】:

    标签: r


    【解决方案1】:

    您可以使用 plyr 包中的 ddply 直接输出到数据框。

        library(plyr)
        t<-ddply(d.aaf, "plot_bid", summarise, mean=round(mean(aaf),2), SD=round(sd(aaf),2))    
        SD<-t$SD
        mean<-t$mean
    

    【讨论】:

    • 太好了,我不知道这个包。非常感谢。
    猜你喜欢
    • 2021-01-06
    • 2015-10-22
    • 2011-11-13
    • 1970-01-01
    • 2020-03-14
    • 2023-03-23
    • 1970-01-01
    • 2018-04-25
    • 2020-04-29
    相关资源
    最近更新 更多