Skip to content

howto calculate column means in data frame

3 messages · Maas James Dr (MED), Joshua Wiley, Berend Hasselman

#
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
[[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
[[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

===============================
Dr. Jim Maas
University of East Anglia
#
Hi Jim,

Using ?lapply with ?colMeans should do the trick.  Here is a little sample:

eg <- list(mtcars, mtcars) # mtcars data frame twice in a list
resultsmean <- lapply(eg, colMeans) # calculate column means for each
element of "eg"
resultsmean # show the results

Hope this helps,

Josh
On Sun, Apr 10, 2011 at 8:27 AM, Maas James Dr (MED) <J.Maas at uea.ac.uk> wrote:

  
    
#
jamaas wrote:
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.