0

I am trying to calculate stock returns on the basis of prices, but since returns will only be calculable from t=2 and so on, I'm one replacement short from being able to assign the returns vector to the original dataframe.

I have assigned NA values to the dframe$returns to begin with, but this hasn't helped. I need a way to come around this issue.

Here's what I have so far:

dframe$returns <- NA
n <- nrows(dframe)
dframe$returns <- (dframe[2:n,adj.price]-dframe[1:(n-1),adj.price])/dframe[1:(n-1),adj.price]

I've tried Google, and I only find people creating return vectors, which is not the real problem, as my issue is how to deal with assigning a vector to a dataframe, where the vector is shorter than the dataframe. Anyone here that can help me?

Skaqqs
  • 4,010
  • 1
  • 7
  • 21

0 Answers0