0

I have a file like this.

head(Historical_Stock_Prices_R)
    Date1     MSFT    AAPL   GOOGL
1 25-01-05 21.02985 4.873362 88.56
2 26-01-05 21.02177 4.886890 94.62
3 27-01-05 21.10259 4.913269 94.04

want to calculate log return using this formula ln(current price/previous price) and my expected output is like this

 Date1        MSFT   AAPL    GOOGL          
26-01-05    -0.04%  0.28%    6.62%
27-01-05     0.38%  0.54%   -0.61% 

tried to solve by this codes from previous stack overflow answer but fail

logs=data.frame( cbind.data.frame(newdates[-1], 

diff(as.matrix(log(Historical_Stock_Prices_R[,-1]))))) 

3 Answers3

0

Try this:

df = read.table(text="
Date1     MSFT    AAPL   GOOGL
1 25-01-05 21.02985 4.873362 88.56
2 26-01-05 21.02177 4.886890 94.62
3 27-01-05 21.10259 4.913269 94.04",header=T)

cbind.data.frame(date=df$Date1[-1],apply(df[,2:4],2,function(x) log(x[-1]/x[-length(x)])*100))

#       date        MSFT      AAPL      GOOGL
# 2 26-01-05 -0.03842896 0.2772061  6.6188582
# 3 27-01-05  0.38372143 0.5383395 -0.6148647
timfaber
  • 2,060
  • 1
  • 15
  • 17
  • After giving this code cbind.data.frame(date=Historical_Stock_Prices_R$Date1[2:3],apply(Historical_Stock_Prices_R[,2:4],2,function(x) log(x[-1]/x[-length(x)])*100)) it shows this Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 2, 2245 > – Syed Riaz Mahmood Ali Jun 02 '17 at 08:29
  • My bad, that would be because I used your example (2 rows) and your matrix is presumably larger. I replaced `df$Date1[2:nrow(df)]` which should do the trick. (mind that df = Historical_Stock_Prices_R) – timfaber Jun 02 '17 at 08:32
  • sorry I understand – Syed Riaz Mahmood Ali Jun 02 '17 at 08:35
  • cbind.data.frame(date=df$Date1[2:nrow(df)],apply(df[,2:4],2,function(x) log(x[-1]/x[-length(x)])*100)) Error in df$Date1 : object of type 'closure' is not subsettable – Syed Riaz Mahmood Ali Jun 02 '17 at 10:14
  • np :) You could replace `paste0(round(log(x[-1]/x[-length(x)])*100,digits=2),"%")` to get your desired output incl. 2 dec points and the `%` sign – timfaber Jun 02 '17 at 10:21
0

It would be helpful if you could be a bit more precise as to what you mean with "fail". Add your error message for example. See more here on good questions

In any case I suspect that if you replace your code with the following it should work.

logs=data.frame( cbind.data.frame(Historical_Stock_Prices_R["Date1",-1], diff(as.matrix(log(Historical_Stock_Prices_R[,-1]))))) 

The reason your code fails is that there probably is no object in your environment called "newdates", so you have to refer to the original dataframe. You can then rename the column afterwards.

Maarten Punt
  • 256
  • 1
  • 11
0

Using TTR package and ROC

> library(TTR)
> stocks
     Date1     MSFT     AAPL GOOGL
1 25-01-05 21.02985 4.873362 88.56
2 26-01-05 21.02177 4.886890 94.62
3 27-01-05 21.10259 4.913269 94.04


> rocMSFT <- ROC(stocks[,"MSFT"])
> rocMSFT
[1]            NA -0.0003842896  0.0038372143

but also look at dailyReturn: calculate daily returns from quantmod http://www.quantmod.com/documentation/periodReturn.html

Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60