[R] simple averaging question?

Chuck Cleland ccleland at optonline.net
Fri Nov 2 00:27:18 CET 2007


Jeff Miller wrote:
> Hi all,

> Suppose I have a column vector of 600 measurements taken in 1s intervals.
> 
> What I want is a new vector with the averages for each min (so there would
> be 10 entries).
> 
> Is there an efficient way to do this? I’ve been doing it with a ‘for’ loop
> but something tells me there is a simple command that is more efficient.

  How about something like this?

y <- runif(600)

df <- data.frame(minute = rep(1:10, each=60), y = y)

with(df, tapply(y, minute, mean))
        1         2         3         4         5
0.4664301 0.4622071 0.5159511 0.4744836 0.5282750
        6         7         8         9        10
0.4670941 0.5091410 0.4648349 0.5227221 0.5251926

> Jeff
> 
>  
> 
> 
> Internal Virus Database is out-of-date.
> Checked by AVG Free Edition. 
> 
> 3:09 PM
> 
> 	[[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.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list