This is my first time with coding - not in finances, either. Just trying to learn R.
I tried to calculate differences in closing price for historical data on Google. The problem is that to apply a function (and I bet there are many functions for this already prepackaged), I could only make it work by converting the Adjusted price column of the xts file into a vector.
I got what I wanted, but now I'm stuck with a vector that I can't merge back into the xts file to look up specific dates:
getSymbols("GOOG")
head(GOOG)
z = as.vector(GOOG$GOOG.Adjusted)
D2D = function (x) {
days = length(x)
delta = numeric(days)
for(i in 2:days){
delta[i] <- (100*((x[i] - x[i - 1])/(x[i - 1])))
}
delta
}
DELTA = D2D(z)
summary(DELTA)
GOOG_DELTA = append(0,DELTA)
merge(GOOG,GOOG_DELTA)
Any tips would be appreciated.