# [R] how to calculate average of each column

arun smartpink111 at yahoo.com
Wed Apr 10 22:13:22 CEST 2013

```Hi,
TRy this:
set.seed(52)
dat1<- as.data.frame(matrix(sample(1:40,100*60,replace=TRUE), nrow=600))

lapply(split(dat1,((seq_len(nrow(dat1))-1)%/% 60)+1),nrow)
#\$`1`
# 60

#\$`2`
# 60

#\$`3`
# 60

res<-lapply(split(dat1,((seq_len(nrow(dat1))-1)%/% 60)+1),colMeans)
res[1:2]
#\$`1`
#      V1       V2       V3       V4       V5       V6       V7       V8
#20.95000 20.53333 20.55000 21.13333 20.10000 18.33333 21.13333 20.50000
#      V9      V10
#20.86667 18.70000

#\$`2`
#      V1       V2       V3       V4       V5       V6       V7       V8
#22.16667 19.85000 19.73333 22.26667 18.80000 19.93333 18.85000 20.46667
#      V9      V10
#17.81667 18.51667

A.K.

----- Original Message -----
From: Ye Lin <yelin at lbl.gov>
To: r-help at r-project.org
Cc:
Sent: Wednesday, April 10, 2013 1:46 PM
Subject: [R] how to calculate average of each column

Hey All,

I have a large dataset and I want to calculate the average of each column
then return a new dataset.

Here is my question: I dont know if there is a function that can allow me
to calculate the average every 60 records of data in the whole dataset, and
return a new data frame. Not sure if I have to divide the dataset first for
every 60, then do the mean or can i directly do that.

cici

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help