[R] howto calculate column means in data frame

Berend Hasselman bhh at xs4all.nl
Sun Apr 10 17:48:10 CEST 2011


jamaas wrote:
> 
> Long story short, I have a big iterative procedure that produces a long
> list of data.frames such as the one called
> "results" here.  Is there an easy way to produce a similar list of
> data.frames comprised of the mean of each of the
> columns in results, such that it ends up like the one I've shown in
> "resultsmean" below?
> 
> I've tried apply and lapply, still not got the correct arguments.  As
> usual, TIA.
> 
> Jim
> 
>> results
> [[1]]
>        name    LOR23       BIA23       MSE23          H0R23
> 1 0.2111122   -1.012228   -0.095937    0.035650        1.00
> 2 0.2111122   -0.836300    0.079991    0.042322        0.75
> 3 0.2111122   -0.518631    0.397659    0.214593        0.50
> 
> 
> [[2]]
>        name    LOR23       BIA23       MSE23          H0R23
> 1 0.2211122   -0.724630    0.191660    0.051308           1
> 2 0.2211122   -0.781812    0.134478    0.033872           1
> 3 0.2211122   -0.522109    0.394181    0.164628        0.75
> 
> 
> would like
> 
>> resultsmean
> [[1]]
>        name    LOR23       BIA23       MSE23          H0R23
> 1 0.2111122   -0.78333    0.12734     0.097160        0.75
> 
> [[2]]
>        name    LOR23       BIA23       MSE23          H0R23
> 1 0.2211122   -0.67566    0.2400      0.08266         0.916
> 

lapply(results, FUN=colMeans)

Berend

--
View this message in context: http://r.789695.n4.nabble.com/howto-calculate-column-means-in-data-frame-tp3439932p3439959.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list