Re: [R] any alternatives for complex for-loops?

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Wed, 12 Nov 2008 10:07:48 -0700

Here is one more approach (just to give more options):

> x <- 1:10
> xx <- embed(x, 2)
> rowMeans(xx)

Or

> complexFn(xx[,2], xx[,1])

Or other variants.

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow_at_imail.org
801.408.8111



> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-
> project.org] On Behalf Of Kinoko
> Sent: Wednesday, November 12, 2008 6:57 AM
> To: r-help_at_r-project.org
> Subject: Re: [R] any alternatives for complex for-loops?
>
> Gentlemen,
>
> Thank you for all your help.
> This list is really amazing.
>
> Following Erik's hint I have managed to find the answer:
>
>
> complexFn <- function(a,b){
> c <- (a+b)/2
> return(c)
> }
>
> x <- zoo(1:10)
> rollapply(x,width=2, function(xx) complexFn(xx[1],xx[2]))
>
> ______________________________________________
> 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.
______________________________________________ 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.
Received on Wed 12 Nov 2008 - 17:09:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 12 Nov 2008 - 17:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive