Search code examples
rfinancequantitative-finance

Introduce a "value_t / value_t-1 -1" formula


I have a .csv table with daily values of a stock and I want to add a column with daily returns. This means that I need to add a column that has this formula:

value_t / value_t-1 - 1

I currently have this:

temp <- read.table("data.csv",header=TRUE,sep=",")
daily <- temp[,2]
dailyreturn <- daily / daily - 1
temp <- cbind(temp, dailyreturn)
write.csv(temp, "daily_return.csv")

As you can see, the code is almost done. It works as intended, I just have no idea how to introduce the formula into my function.

Thanks in advance! And sorry for such a noob question :-)


Solution

  • Try replacing the third line with following and see if that is what you want.

    dailyreturn <- c(NA,daily[-1]/daily[-length(daily)]-1)