[R] how to calculate average of each column

arun smartpink111 at yahoo.com
Wed Apr 10 23:05:41 CEST 2013


Hi,

Is this what you need?
as.data.frame(do.call(rbind,res))
#         V1       V2       V3       V4       V5       V6       V7       V8
#1  20.95000 20.53333 20.55000 21.13333 20.10000 18.33333 21.13333 20.50000
#2  22.16667 19.85000 19.73333 22.26667 18.80000 19.93333 18.85000 20.46667
#3  21.90000 20.15000 19.36667 20.20000 19.68333 21.36667 19.55000 21.63333
#4  21.10000 17.53333 22.43333 18.16667 21.45000 21.20000 19.51667 20.30000
#5  20.35000 19.30000 20.06667 19.25000 22.08333 23.03333 22.25000 19.15000
#6  19.36667 20.88333 20.86667 19.73333 22.66667 21.50000 21.86667 17.50000
#7  19.08333 22.06667 20.75000 20.41667 22.76667 20.83333 21.65000 19.85000
#8  20.56667 23.28333 20.81667 20.36667 21.16667 20.10000 21.00000 19.91667
#9  18.93333 22.38333 19.41667 18.50000 21.70000 22.80000 19.23333 20.68333
#10 22.40000 19.56667 21.05000 20.16667 17.41667 19.85000 18.95000 19.86667
  #       V9      V10
#1  20.86667 18.70000
#2  17.81667 18.51667
#3  20.50000 21.98333
#4  19.65000 20.13333
#5  21.21667 22.15000
#6  21.96667 21.85000
#7  20.81667 16.81667
#8  21.08333 21.26667
#9  22.30000 19.25000
#10 22.73333 21.53333
A.K.



________________________________
 From: Ye Lin <yelin at lbl.gov>
To: arun <smartpink111 at yahoo.com> 
Cc: R help <r-help at r-project.org> 
Sent: Wednesday, April 10, 2013 4:50 PM
Subject: Re: [R] how to calculate average of each column
 

Thanks so much! it works fine!


But when I convert the result to a data frame, it mess up.


so i have:


res <- data.frame(res)


then i get:

X1       X2       X3       X4       X5       X6
V1  20.95000 22.16667 21.90000 21.10000 20.35000 19.36667
V2  20.53333 19.85000 20.15000 17.53333 19.30000 20.88333
V3  20.55000 19.73333 19.36667 22.43333 20.06667 20.86667
V4  21.13333 22.26667 20.20000 18.16667 19.25000 19.73333
V5  20.10000 18.80000 19.68333 21.45000 22.08333 22.66667
V6  18.33333 19.93333 21.36667 21.20000 23.03333 21.50000
V7  21.13333 18.85000 19.55000 19.51667 22.25000 21.86667
V8  20.50000 20.46667 21.63333 20.30000 19.15000 17.50000
V9  20.86667 17.81667 20.50000 19.65000 21.21667 21.96667
V10 18.70000 18.51667 21.98333 20.13333 22.15000 21.85000 X7       X8       X9      X10
V1  19.08333 20.56667 18.93333 22.40000
V2  22.06667 23.28333 22.38333 19.56667
V3  20.75000 20.81667 19.41667 21.05000
V4  20.41667 20.36667 18.50000 20.16667
V5  22.76667 21.16667 21.70000 17.41667
V6  20.83333 20.10000 22.80000 19.85000
V7  21.65000 21.00000 19.23333 18.95000
V8  19.85000 19.91667 20.68333 19.86667
V9  20.81667 21.08333 22.30000 22.73333
V10 16.81667 21.26667 19.25000 21.53333


how can i return a final output like this:


with original column name

10 rows of each average record


Thanks!





On Wed, Apr 10, 2013 at 1:13 PM, arun <smartpink111 at yahoo.com> wrote:

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