[R] how to calculate average of each column

Rui Barradas ruipbarradas at sapo.pt
Wed Apr 10 22:54:43 CEST 2013


You should provide us with a data example, like the posting guide asks 
you to.
Without one, you could adapt the following example to your case

# Make up some data
dat <- data.frame(X = rnorm(200), Y = rnorm(200))

# Divide into subsets of 60 rows each and compute the col means
grp <- rep(1:(1 + nrow(dat) / 60), each = 60)[seq_len(nrow(dat))]
do.call(rbind, lapply(split(dat, grp), colMeans))

Hope this helps,

Rui Barradas

Em 10-04-2013 18:46, Ye Lin escreveu:
> 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