Hello All: If I do have: x = (2, 4, 5, 5, 6, 4, 5, 2, 1) y = (9, 11.5, 12.5, 13, 14, 19, 20, 21, 22) I wanted to find a simple function in R which calculates the averages of X and Y for every 4 unit increase of Y. The results should look like: x = (4, 6, 3) #where (2+ 4+ 5+ 5)/ 4 = 4 and ... y = (12, 14, 20.5) #where (9+11.5+12.5+13)/ 4 = 12 and ... Thanks, Mohsen [[alternative HTML version deleted]]
you mean something along the lines of filter(x, rep(1/4, 4)) (which you can combine with na.omit) ? On Wed, Feb 17, 2010 at 5:18 PM, Mohsen Jafarikia <jafarikia at gmail.com> wrote:> Hello All: > > If I do have: > > x = (2, 4, 5, 5, 6, 4, 5, 2, 1) > y = (9, 11.5, 12.5, 13, 14, 19, 20, 21, 22) > > I wanted to find a simple function in R which calculates the averages of X > and Y for every 4 unit increase of Y. The results should look like: > > x = (4, 6, 3) ? ? ? ? ? ? ? #where (2+ 4+ 5+ 5)/ 4 = 4 and ... > y = (12, 14, 20.5) ? ? ?#where (9+11.5+12.5+13)/ 4 = 12 and ... > > Thanks, > Mohsen > > ? ? ? ?[[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. >
Hi: Alternatively, you could use rollmean in the zoo package: library(zoo) x <- c(2, 4, 5, 5, 6, 4, 5, 2, 1) rollmean(x, 4) [1] 4.00 5.00 5.00 5.00 4.25 3.00 which avoids the NAs that get produced from filter: filter(x, rep(1/4, 4)) Time Series: Start = 1 End = 9 Frequency = 1 [1] NA 4.00 5.00 5.00 5.00 4.25 3.00 NA NA HTH, Dennis On Wed, Feb 17, 2010 at 9:18 AM, Mohsen Jafarikia <jafarikia@gmail.com>wrote:> Hello All: > > If I do have: > > x = (2, 4, 5, 5, 6, 4, 5, 2, 1) > y = (9, 11.5, 12.5, 13, 14, 19, 20, 21, 22) > > I wanted to find a simple function in R which calculates the averages of X > and Y for every 4 unit increase of Y. The results should look like: > > x = (4, 6, 3) #where (2+ 4+ 5+ 5)/ 4 = 4 and ... > y = (12, 14, 20.5) #where (9+11.5+12.5+13)/ 4 = 12 and ... > > Thanks, > Mohsen > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]