[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`
#[1] 60

#$`2`
#[1] 60

#$`3`
#[1] 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.

thanks for your help!


cici

    [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list