开发者

R question - How to extract attributes values from bystat object and place them in variables

I'm using the bystat function from the Hmisc package in R. How can I开发者_如何学编程 extract attribute values and place them into variables. For example, I want to calculate mean and SD for variable aaf and put them in a dataframe or matrix.

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"

The way I'm doing it is by first converting "t" into a dataframe, which I do not think is very efficient. Thanks for your suggestions.


You could use ddply from the plyr package which outputs directly to a data frame.

    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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜