Dear all, i have daily stock prices for more than 10 years and want to compute annual volatilities for certain dates during this period. Since i have found no "easy" way to work with time data, the data presents itself in the structure TIme Index - Stock Price 1 - 15,6 2 - 17 ... ... 2010 - 28 2011- 28,5 ... 4500 - 23 Since I want to have the volatility only for certain dates, I define these dates in a vector Deal_Dates <- c(500,600,700,805) and use the for-command to calculate these specific volatities via for(i in Deal_Dates){Volatility(i)} My problem is now, that I want to save the results in a new matrix which i created in the structure results <- matrix(rep(NA,4*2),4,2) in a way that Time Index - Volatility 500 - 0.55 600 - 0.44 700 - 0.45 805 - 0.66 is stored in the results matrix. How can I save the result of each for-loop in the matrix? I want the respective Time-Index of the loop to be put in the first column and the volatility in the second column. Since my vector for the for-loop consists of "random" values for the Time-Index, i can't write resultsl[i,1] = Time Index and results[i,2] = Volatility I am looking for something that gives me the current round or iteration of the for-loop in order to replace the "i" in adressing the cells of the result matrix. Hope you can help! -- View this message in context: http://r.789695.n4.nabble.com/Counting-the-loop-round-of-a-for-loop-tp4381319p4381319.html Sent from the R help mailing list archive at Nabble.com.
Hi, You can initialize a counter and update it in the loop. An silly example (unrelated to yours because it was not reproducible) of this technique is: x <- matrix( , ncol = 2, nrow = 26) n <- 0 for(i in letters) { n <- n+1 x[n,] <- c(i, n) } Best, Ista On Sunday, February 12, 2012 07:17:33 AM jolo999 wrote:> Dear all, > > i have daily stock prices for more than 10 years and want to compute annual > volatilities for certain dates during this period. Since i have found no > "easy" way to work with time data, the data presents itself in the structure > > TIme Index - Stock Price > 1 - 15,6 > 2 - 17 > ... > ... > 2010 - 28 > 2011- 28,5 > ... > 4500 - 23 > > Since I want to have the volatility only for certain dates, I define these > dates in a vector > > Deal_Dates <- c(500,600,700,805) > > and use the for-command to calculate these specific volatities via > > for(i in Deal_Dates){Volatility(i)} > > My problem is now, that I want to save the results in a new matrix which i > created in the structure > > results <- matrix(rep(NA,4*2),4,2) in a way that > > Time Index - Volatility > 500 - 0.55 > 600 - 0.44 > 700 - 0.45 > 805 - 0.66 > is stored in the results matrix. > > How can I save the result of each for-loop in the matrix? I want the > respective Time-Index of the loop to be put in the first column and the > volatility in the second column. Since my vector for the for-loop consists > of "random" values for the Time-Index, i can't write > > resultsl[i,1] = Time Index and > results[i,2] = Volatility > > I am looking for something that gives me the current round or iteration of > the for-loop in order to replace the "i" in adressing the cells of the > result matrix. > > Hope you can help! > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Counting-the-loop-round-of-a-for-loop-tp43813 > 19p4381319.html Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
It seems to work. Simple and effective! Thanks! -- View this message in context: http://r.789695.n4.nabble.com/Counting-the-loop-round-of-a-for-loop-tp4381319p4381780.html Sent from the R help mailing list archive at Nabble.com.
Consider using sapply instead of a for loop, if the code in the sapply call returns a vector, and every vector is the same length, then sapply will automatically form it into a matrix for you. On Sun, Feb 12, 2012 at 12:30 PM, jolo999 <jonas.lorson at ebs.de> wrote:> It seems to work. Simple and effective! > > Thanks! > > -- > View this message in context: http://r.789695.n4.nabble.com/Counting-the-loop-round-of-a-for-loop-tp4381319p4381780.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com
Maybe Matching Threads
- EMM: how to make forecast using EMM methods?
- where do I find stochastic volatilities models in R or Matlab?
- Package "demography" - calculating quintiles of survival probabilities
- How to do a target value search analogous to Excel Solver
- how to get the position of an element in a vector ?