Calculate returns on a daily basis in R -


need calculate returns each company’s share given year on daily basis.

date         amzn         goog      wfm          msft 4/1/2016    636.98999   741.840027  33.27       54.799999 5/1/2016    633.789978  742.580017  33.650002   55.049999 6/1/2016    632.650024  743.619995  33.43       54.049999 7/1/2016    607.940002  726.390015  32.5        52.169998 8/1/2016    607.049988  714.469971  31.98       52.330002 

the data read csv file , stored in data frame mydf. calculate daily returns, need perform below calculation - (price of (5/1) - price of (4/1))/(price of (4/1))

how make recurring entries in data frame? obtain difference using diff(mydf$amzn)

try this:

cbind(mydf[-1,1],apply(mydf[,-1],2,function(x) diff(x)/head(x,-1))) 

output:

       date         amzn          goog          wfm         msft 1: 5/1/2016 -0.005023646  0.0009975062  0.011421761  0.004562044 2: 6/1/2016 -0.001798631  0.0014004928 -0.006537949 -0.018165305 3: 7/1/2016 -0.039057964 -0.0231704098 -0.027819324 -0.034782628 4: 8/1/2016 -0.001463983 -0.0164099778 -0.016000000  0.003066973 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -