tags:

views:

32

answers:

1

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.

+1  A: 

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
Brian
Excellent, I wasn't aware of this package. Thank you very much.
Fernando