I downloaded several timeseries via Yahoo-Finance, Reuters and other sources.
They are all listed as seperate "xts"-objects which contains the respective closing price. These vectors are available for daily and monthly intervals.
I would like to create one chart which shows the price developement of my stocks. This chart should the relative price developement in relation to the first day:
price of 2005-01-04/price of 2005-01-03
price of 2005-01-05/price of 2005-01-03
and so on.
For this I tried to create a for-loop:
indexfun <- function(x)
{
y <- as.matrix(x)
z <- rep(NULL, nrow(x))
for(i in nrow(y)){
z[i] <- y[i,1]/y[1,1]
print(z)
}
}
Unfortunately, it returns soley NA-values except for the last one. I tried to save the vector as matrix to ensure I can acces the column containing the closing prices and leaving the dates untouched.
My xts-vector looks like
BA.close
2005-01-03 50.97
2005-01-04 49.98
2005-01-05 50.81
2005-01-06 50.48
2005-01-07 50.31
2005-01-10 50.98
Can you help me?
Thank you very much.
for(i in 1:nrow(y))
. – Lyngbakrdput()
function (or just a sample)? – Mike