[R] Question about Reduce

Charles C. Berry cberry at tajo.ucsd.edu
Fri Oct 1 18:38:39 CEST 2010


On Fri, 1 Oct 2010, Dimitri Liakhovitski wrote:

> Hello!
>
> In the example below the Reduce() function by default assigns "a" to
> be the last accumulated value and "b" to be the current value in "x".
> I could not find this documented anywhere as the default settings for
> the Reduce() function.  Does any sort of documentation for this
> behavior exist?

Yes.

Under 'Details' on the Reduce help page:

 	"... a left reduce computes l_1 = f(v_1, v_2), l_2 = f(l_1, v_3),
 		etc..."

HTH,

Chuck
>
> x <-  c(0,0,0,0,0,1,0,0,0,5,0,0,0,7,0,0,0,8,5,10)
> Reduce(function(a,b) b + (a * 0.5),x,accumulate=T,init=0)
>
> [1]  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000
> 1.0000000  0.5000000  0.2500000
> [10]  0.1250000  5.0625000  2.5312500  1.2656250  0.6328125  7.3164062
> 3.6582031  1.8291016  0.9145508
> [19]  8.4572754  9.2286377 14.6143188
>
>
> Thanks a lot!
>
> -- 
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list