On 02-Nov-07 00:12:19, 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.A simple-minded thought is (constructing first such a vector S): S<-rnorm(600) colMeans(matrix(S,nrow=60)) [1] -0.0999271 0.1622274 0.2044960 -0.1503812 -0.0224353 [6] 0.1133577 0.2567317 -0.2510650 -0.1219953 0.0001626 Hoping this helps! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 01-Nov-07 Time: 23:29:09 ------------------------------ XFMail ------------------------------
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. Jeff Internal Virus Database is out-of-date. Checked by AVG Free Edition. 3:09 PM [[alternative HTML version deleted]]
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
If x is your vector, one possibility is: y <- matrix(x,nrow=60) z <- apply(y,2,mean) --- Jeff Miller <jeffmiller at alphapoint05.net> 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. > > > > 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. >
Moshe, Ted, & Chuck, All THREE approaches worked fine for me. Thank you! Sincerely, Jeff Miller Internal Virus Database is out-of-date. Checked by AVG Free Edition. 3:09 PM -------------- next part -------------- An embedded message was scrubbed... From: "Chuck Cleland" <ccleland at optonline.net> Subject: Re: [R] simple averaging question? Date: Thu, 1 Nov 2007 19:27:18 -0400 Size: 4405 Url: https://stat.ethz.ch/pipermail/r-help/attachments/20071101/947a9943/attachment.mht