0

My data contains the closing prices of 10 shares of the S&P 500 index.

Data :

> dput(head(StocksData))
structure(list(ACE = c(56.86, 56.82, 56.63, 56.39, 55.97, 55.23
), AMD = c(8.47, 8.77, 8.91, 8.69, 8.83, 9.19), AFL = c(51.83, 
50.88, 50.78, 50.5, 50.3, 49.65), APD = c(81.59, 80.38, 80.03, 
79.61, 79.76, 79.77), AA = c(15.12, 15.81, 15.85, 15.66, 15.71, 
15.78), ATI = c(53.54, 52.37, 52.53, 51.91, 51.32, 51.45), AGN = c(69.77, 
69.53, 69.69, 69.98, 68.99, 68.75), ALL = c(29.32, 29.03, 28.99, 
28.66, 28.47, 28.2), MO = c(20.09, 20, 20.07, 20.16, 20, 19.88
), AMZN = c(184.22, 185.01, 187.42, 185.86, 185.49, 184.68)), row.names = c(NA, 
6L), class = "data.frame")

For each of the 10 shares, i want to calculate their daily percentage changes.

I am struggling with this because in this data i havent information about time , i havent info about year , month or days . Any thoughts on this would be helpful.

1 Answers1

0

You can use :

perc_change <- (StocksData[-1, ] - StocksData[-nrow(StocksData), ])/StocksData[-nrow(StocksData), ] * 100
perc_change

#    ACE  AMD   AFL    APD    AA   ATI   AGN   ALL    MO  AMZN
#2 -0.07  3.5 -1.83 -1.483  4.56 -2.19 -0.34 -0.99 -0.45  0.43
#3 -0.33  1.6 -0.20 -0.435  0.25  0.31  0.23 -0.14  0.35  1.30
#4 -0.42 -2.5 -0.55 -0.525 -1.20 -1.18  0.42 -1.14  0.45 -0.83
#5 -0.74  1.6 -0.40  0.188  0.32 -1.14 -1.41 -0.66 -0.79 -0.20
#6 -1.32  4.1 -1.29  0.013  0.45  0.25 -0.35 -0.95 -0.60 -0.44

Note that you have 1 row less than your original dataframe since we don't have previous value to compare for 1st day.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213