Skip to content
Prev 275383 / 398503 Next

new to R coding.

I believe I already showed you how to do this (though your code
doesn't seem to do what your words ask, so I'm not sure which one to
answer), but didn't draw attention to it:

x[-1]/x[-length(x)]

This creates two vectors, one consisting of everything but the first
element of x and the other of everything but the last, and divides
them, which gives the consecutive quotients you want.

Also, if you would, next time you provide sample data (which is a
wonderful thing to do) -- provide it in a directly pastable format
(i.e., with commas for c()). The easiest way to get such data is just
to dput(x) and copy the output.

E.g., for this data I have lying around:


P = c(128.59, 128.87, 127.84, 128.43, 128.32, 128.75, 129.31, 130.29,
130.04, 130.09, 129.66, 129.91, 129.51, 129.89, 130.94, 129.4,
129.28, 129.21, 129.88, 130.76, 131.54, 131.72, 131.76, 131.79,
132.09, 131.91, 130.93, 130.49, 131.59, 132.45, 132.62, 132.56,
132.84, 132.79, 132.69, 132.17, 132.05, 126.89, 128.19, 127.81,
126.14, 124.94, 127.07, 126.95, 128.02, 128.06, 128.25, 125.76,
126.69, 126.86, 126.51, 128.03, 128.74, 130.86, 130.76, 130.95,
130.77, 130.46, 129.51, 129.65, 129.68, 129.82, 131.22, 131.37,
131.72, 131.91, 132.06, 131.52, 132.11, 132.71, 133.97, 134.33,
134.49, 134.45, 135.72, 135.21, 135.27, 136.51, 136.66, 136.56,
135.42, 135.77, 136.56, 137.3, 137.82, 137.85, 137.67, 138.04,
136.6, 137.77, 137.47, 137.5, 138.45, 138.17, 139.38, 139.3,
139.19, 139.21, 137.95, 138.53)

we get

head(P[-1]/P[-length(P)])
1.0021775 0.9920074 1.0046151 0.9991435 1.0033510 1.0043495

which I believe is what you are after.

Michael
On Mon, Oct 24, 2011 at 12:29 PM, tynashy <tynashy at yahoo.co.uk> wrote: