[R] new to R coding.

R. Michael Weylandt michael.weylandt at gmail.com
Mon Oct 24 20:14:25 CEST 2011


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:
>
> I am new to R coding and I am trying to model the returns on the ftse100
> since 1990. I have got a vector with all the closing values on each trading
> day. however, instead of using the difference in the closing values of two
> consecutive days, (ie dx=diff(x) where x is the vector containing the
> closing values), i wanted to use the quotient of the two closing values. I
> have tried the following without any luck
>
>> x = c (2434.1 2463.7 2451.6 2444.5 2431.3 2436.3 2412.6 2417.9 2380.1
>> 2366.2
>  2349.1 2373.9 2336.9 2335.0 2297.1 2291.1 2278.6 2289.9 2314.5 2328.8
>  2322.0 2337.3 2345.8 2355.1 2348.4 2321.1 2307.4 2331.0 2313.6 2286.9
>  2293.2 2298.3 2313.8 2325.9 2297.1 2277.0 2259.7 2269.2 2236.7 2249.3
>  2254.8 2255.4 2238.4 2254.8 2230.5 2216.0 2230.3 2250.0 2234.3 2222.8
>  2224.5 2226.1 2234.9 2263.9 2238.0 2259.7 2250.3 2258.9 2283.9 2298.2
>  2266.2 2275.0 2263.0 2247.9 2221.6 2240.7 2231.6 2239.5 2221.1)
> #extraction of the stock index returns
>
>> n=length(x)
>> d=diff (log(x)) # daily log returns
>
>> for (i in 2:n) {
> + dx[i]=(d[i])/(d[i-1])
> + delta=dx[i]
> + } # this is what I have tried to do.
> how do i do this properly in R please.
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/new-to-R-coding-tp3933588p3933738.html
> Sent from the R help mailing list archive at Nabble.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.
>



More information about the R-help mailing list