Hi,
You can do this:
> x[-1]*x[-length(x)]
[1] 0.02 0.06 0.12 0.20 0.30 0.42 0.56 0.72 0.90 1.10 1.32 1.56 1.82
2.10 2.40
[16] 2.72 3.06 3.42 3.80
In (recent version of) R, you could do:
> head(x,-1)*tail(x,-1)
[1] 0.02 0.06 0.12 0.20 0.30 0.42 0.56 0.72 0.90 1.10 1.32 1.56 1.82
2.10 2.40
[16] 2.72 3.06 3.42 3.80
Cheers,
Romain
--
Mango Solutions
data analysis that delivers
Introduction to R training course :: London :: 06-07 March 2008
http://www.mango-solutions.com/services/rtraining/r_intro.html
John Pitchard wrote:
Dear All,
I want to produce a running cumulative product. For example, if I have
x <- seq(0.1, 2, 0.1)
> x
[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6
1.7 1.8 1.9 2.0
Then I want a sequence taking each pair of numbers, i.e. 0.1 and 0.2
(calculate the product), 0.2 and 0.3 (calculate the product), 0.3 and
0.4 (calculate the product),..., 1.9 and 2.0 (calculate the product).
So I get a vector of length 18 in this example (20 -2). The vector
will look something like this:
0.02 0.06.... 3.8
Does anyone know an efficient way to generate this sequence?
Many thanks in advance.
Regards,
John
|